Skip to content

Fix TestTypeHints.test_doc_examples#56388

Closed
samestep wants to merge 4 commits intopytorch:masterfrom
samestep:test-type-hints-cwd-test
Closed

Fix TestTypeHints.test_doc_examples#56388
samestep wants to merge 4 commits intopytorch:masterfrom
samestep:test-type-hints-cwd-test

Conversation

@samestep
Copy link
Copy Markdown
Contributor

@samestep samestep commented Apr 19, 2021

#54268 removed test_run_mypy since now we're running mypy as its own job in GitHub Actions, but previously we used this set_cwd context manager in that test to ensure that we picked up the mypy config correctly. However, for some reason, we have not been doing that in test_doc_examples, which has been succeeding in CI for a while despite being broken.

Specifically, run_test.py changes the working directory to test/ before running test files, which is contrary to what CONTRIBUTING.md instructs developers to do. As a result, in CI, test/test_type_hints.py has been passing in CI, but if you run it locally from the root of the repo, this you get this error:

F
======================================================================
FAIL: test_doc_examples (__main__.TestTypeHints)
Run documentation examples through mypy.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test/test_type_hints.py", line 127, in test_doc_examples
    self.fail(f"mypy failed:\n{stdout}")
AssertionError: mypy failed:
test/generated_type_hints_smoketest.py:851: error: Name 'tensor' is not defined  [name-defined]
test/generated_type_hints_smoketest.py:853: error: Name 'tensor' is not defined  [name-defined]
Found 2 errors in 1 file (checked 1 source file)


----------------------------------------------------------------------
Ran 1 test in 1.416s

FAILED (failures=1)

Test plan:

Before this PR, the first of the following two commands should fail (since that is essentially what is run in CI), but the second should fail:

python test/run_test.py -i test_type_hints
python test/test_type_hints.py

After this PR, both commands should succeed.

@facebook-github-bot
Copy link
Copy Markdown
Contributor

facebook-github-bot commented Apr 19, 2021

💊 CI failures summary and remediations

As of commit 3031505 (more details on the Dr. CI page):


💚 💚 Looks good so far! There are no failures yet. 💚 💚


This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions to the (internal) Dr. CI Users group.

@samestep samestep changed the title Set cwd to repo root dir in test_doc_examples Fix test_doc_examples Apr 19, 2021
@samestep samestep changed the title Fix test_doc_examples Fix TestTypeHints.test_doc_examples Apr 19, 2021
@samestep samestep changed the title Fix TestTypeHints.test_doc_examples Fix TestTypeHints.test_doc_examples Apr 19, 2021
@samestep samestep changed the title Fix TestTypeHints.test_doc_examples Fix TestTypeHints.test_doc_examples Apr 19, 2021
@samestep samestep marked this pull request as ready for review April 19, 2021 18:26
@samestep samestep requested review from a team and rgommers April 19, 2021 18:27
@facebook-github-bot
Copy link
Copy Markdown
Contributor

@samestep has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

# entire process!
with set_cwd(str(repo_rootdir)):
(stdout, stderr, result) = mypy.api.run([
'--cache-dir=.mypy_cache/doc',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like it'd be cleaner to use the --config-file option and pass it an absolute path to the mypy.ini rather than changing the cwd, would that still work?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that doesn't immediately work (it requires modifications to mypy_plugins/check_mypy_version.py), and I also simply prefer this because it matches up with the way we otherwise use mypy in CI (from the repo root)

@samestep samestep requested a review from a team April 19, 2021 18:32
Co-authored-by: Rong Rong <walterddr.walterddr@gmail.com>
@facebook-github-bot
Copy link
Copy Markdown
Contributor

@samestep has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Comment on lines +122 to +124
repo_rootdir = Path(__file__).resolve().parent.parent
# TODO: Would be better not to chdir here, this affects the
# entire process!
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please also include the github URL for the issue you created to discuss this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 19, 2021

Codecov Report

Merging #56388 (3031505) into master (b387f7c) will decrease coverage by 0.00%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##           master   #56388      +/-   ##
==========================================
- Coverage   77.03%   77.03%   -0.01%     
==========================================
  Files        1924     1924              
  Lines      190583   190583              
==========================================
- Hits       146819   146814       -5     
- Misses      43764    43769       +5     

@facebook-github-bot
Copy link
Copy Markdown
Contributor

@samestep merged this pull request in 34d0bd5.

@rgommers rgommers added the module: typing Related to mypy type annotations label Apr 20, 2021
@samestep samestep deleted the test-type-hints-cwd-test branch April 20, 2021 21:28
krshrimali pushed a commit to krshrimali/pytorch that referenced this pull request May 19, 2021
Summary:
pytorch#54268 removed `test_run_mypy` since now we're running `mypy` as its own job in GitHub Actions, but previously we used this `set_cwd` context manager in that test to ensure that we picked up the `mypy` config correctly. However, for some reason, we have not been doing that in `test_doc_examples`, which has been succeeding in CI for a while despite being broken.

Specifically, [`run_test.py` changes the working directory to `test/` before running test files](https://github.com/pytorch/pytorch/blob/48aaea3359cffe7982ce24b8742c7a1f4b456a92/test/run_test.py#L534-L535), which is contrary to [what `CONTRIBUTING.md` instructs developers to do](https://github.com/pytorch/pytorch/blob/48aaea3359cffe7982ce24b8742c7a1f4b456a92/CONTRIBUTING.md#python-unit-testing). As a result, in CI, `test/test_type_hints.py` has been passing in CI, but if you run it locally from the root of the repo, this you get this error:
```
F
======================================================================
FAIL: test_doc_examples (__main__.TestTypeHints)
Run documentation examples through mypy.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test/test_type_hints.py", line 127, in test_doc_examples
    self.fail(f"mypy failed:\n{stdout}")
AssertionError: mypy failed:
test/generated_type_hints_smoketest.py:851: error: Name 'tensor' is not defined  [name-defined]
test/generated_type_hints_smoketest.py:853: error: Name 'tensor' is not defined  [name-defined]
Found 2 errors in 1 file (checked 1 source file)

----------------------------------------------------------------------
Ran 1 test in 1.416s

FAILED (failures=1)
```

Pull Request resolved: pytorch#56388

Test Plan:
Before this PR, the first of the following two commands should fail (since that is essentially what is run in CI), but the second should fail:
```
python test/run_test.py -i test_type_hints
python test/test_type_hints.py
```
After this PR, both commands should succeed.

Reviewed By: driazati

Differential Revision: D27860173

Pulled By: samestep

fbshipit-source-id: efb82fffd7ccb04d0331824b40bdef7bbc319c98
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed Merged module: typing Related to mypy type annotations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants