Skip to content

Add --check-untyped-defs to mypy.ini and test suite#37594

Closed
rgommers wants to merge 9 commits intopytorch:masterfrom
rgommers:mypy-test
Closed

Add --check-untyped-defs to mypy.ini and test suite#37594
rgommers wants to merge 9 commits intopytorch:masterfrom
rgommers:mypy-test

Conversation

@rgommers
Copy link
Copy Markdown
Collaborator

Also move the ignores for imports to the bottom in mypy.ini, those are much less interesting - start with the stuff people want to work on.

Second commit tests the instructions: remove an ignore, fix the issue.

Also move the ignores for imports to the bottom in mypy.ini,
those are much less interesting - start with the stuff people
want to work on.
Comment thread mypy-README.md Outdated
2. Inline type annotations for all Python code, _except_ if there are too many
overloads for functions/methods - in that case a stub file should be
preferred (what's too many is a bit of a judgement call, I'd suggest two per
function is a reasonable threshold).
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.

One of the benefits of type hints is that they are self-documenting. For this reason, I'd personally be a bit more aggressive in the number of permissible @overloads permitted before going to stubs. Unfortunately, I don't have a firm number in mind.

If feasible, what might be beneficial is an analysis of the distribution of @overloads per function/method to inform the decision.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Right now I don't see any that exceed the threshold. torch/nn/modules/container.pyi has some double overloads (__init__, __getitem__). I expect the number of overloads to grow significantly though when people want to make signatures more specific - things like Union[Iterable[Tensor], Iterable[dict]] and Union[int, List[int], Size] may turn into overloads.

That said, point taken, I'll bump it to 3.

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.

#35566 is another case where we may not be able to put things in code, and have to move it to stubs.

Comment thread mypy-README.md Outdated
@ezyang
Copy link
Copy Markdown
Contributor

ezyang commented May 1, 2020

This LGTM. @rgommers I'll let you make the small updates first? Another place these docs could live is in the wiki, where you don't have to go through landing cycle as is the case in the main repo.

@rgommers
Copy link
Copy Markdown
Collaborator Author

rgommers commented May 1, 2020

I'll let you make the small updates first?

yep

Another place these docs could live is in the wiki, where you don't have to go through landing cycle as is the case in the main repo.

That makes sense to me. I only used this because mypy-README already existed. It looks a bit out of place at the root of the repo; happy to delete it completely in this PR and move the content to the wiki.

@ezyang
Copy link
Copy Markdown
Contributor

ezyang commented May 1, 2020

That makes sense to me. I only used this because mypy-README already existed. It looks a bit out of place at the root of the repo; happy to delete it completely in this PR and move the content to the wiki.

SGTM!

@ngimel ngimel added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label May 5, 2020
@rgommers rgommers requested a review from apaszke as a code owner May 6, 2020 15:24
@rgommers rgommers changed the title Document how to run and improve type annotations better Add --check-untyped-defs to mypy.ini and test suite May 6, 2020
@dr-ci
Copy link
Copy Markdown

dr-ci Bot commented May 6, 2020

💊 Build failures summary and remediations

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


  • 1/1 failures introduced in this PR

🕵️ 1 new failure recognized by patterns

The following build failures do not appear to be due to upstream breakages:

See CircleCI build pytorch_linux_xenial_py3_6_gcc5_4_ge_config_profiling_test (1/1)

Step: "Run tests" (full log | diagnosis details | 🔁 rerun)

May 06 21:13:22 NameError: name 'enable_profiling_mode' is not defined
May 06 21:13:22   test_init_ops (jit.test_unsupported_ops.TestUnsupportedOps) ... ok (0.019s) 
May 06 21:13:22   test_ops_bound_in_functional (jit.test_unsupported_ops.TestUnsupportedOps) ... ok (0.011s) 
May 06 21:13:22   test_tensor_options_behavior_mismatch (jit.test_unsupported_ops.TestUnsupportedOps) ... ok (0.003s) 
May 06 21:13:22  
May 06 21:13:22 ====================================================================== 
May 06 21:13:22 ERROR [0.006s]: test_profiling_merge (test_jit.TestScript) 
May 06 21:13:22 ---------------------------------------------------------------------- 
May 06 21:13:22 Traceback (most recent call last): 
May 06 21:13:22   File "/var/lib/jenkins/workspace/test/test_jit.py", line 3623, in test_profiling_merge 
May 06 21:13:22     with enable_profiling_mode(): 
May 06 21:13:22 NameError: name 'enable_profiling_mode' is not defined 
May 06 21:13:22  
May 06 21:13:22 ---------------------------------------------------------------------- 
May 06 21:13:22 Ran 2457 tests in 146.152s 
May 06 21:13:22  
May 06 21:13:22 FAILED (errors=1, skipped=68, expected failures=1) 
May 06 21:13:22  
May 06 21:13:22 Generating XML reports... 
May 06 21:13:22 Generated XML report: test-reports/python-unittest/TEST-jit.test_async.TestAsync-20200506211056.xml 
May 06 21:13:22 Generated XML report: test-reports/python-unittest/TEST-jit.test_autodiff_subgraph_slicing.TestAutodiffSubgraphSlicing-20200506211056.xml 
May 06 21:13:22 Generated XML report: test-reports/python-unittest/TEST-jit.test_builtins.TestBuiltins-20200506211056.xml 

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 on the GitHub issue tracker.

See how this bot performed.

This comment has been revised 13 times.

Copy link
Copy Markdown
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@ezyang is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@rgommers
Copy link
Copy Markdown
Collaborator Author

rgommers commented May 6, 2020

While finishing the "task list" for completing the type annotations I found it was better to add --check-untyped-defs to mypy.ini and the test suite now, so I changed the scope of this PR. There were another ~500 errors that that uncovered, so I added a bunch more ignore_errors = True.

The guide of how to add type annotations and ~180 tasks for it are now in https://github.com/pytorch/pytorch/wiki/Guide-for-adding-type-annotations-to-PyTorch. Please have a look at that as well if you see any fundamental problem.

I think when this PR is merged we're good to start tackling those tasks.

@facebook-github-bot
Copy link
Copy Markdown
Contributor

@ezyang merged this pull request in 46ed334.

laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
Summary:
Also move the ignores for imports to the bottom in `mypy.ini`, those are much less interesting - start with the stuff people want to work on.

Second commit tests the instructions: remove an ignore, fix the issue.
Pull Request resolved: pytorch#37594

Differential Revision: D21434858

Pulled By: ezyang

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

Labels

Merged module: typing Related to mypy type annotations open source triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants