mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
Summary: Closes https://github.com/pytorch/pytorch/issues/50513 by resolving all four checkboxes. If this PR is merged, I will also modify one or both of the following wiki pages to add instructions on how to use this `mypy` wrapper for VS Code editor integration: - [Guide for adding type annotations to PyTorch](https://github.com/pytorch/pytorch/wiki/Guide-for-adding-type-annotations-to-PyTorch) - [Lint as you type](https://github.com/pytorch/pytorch/wiki/Lint-as-you-type) Pull Request resolved: https://github.com/pytorch/pytorch/pull/50826 Test Plan: Unit tests for globbing function: ``` python test/test_testing.py TestMypyWrapper -v ``` Manual checks: - Uninstall `mypy` and run `python test/test_type_hints.py` to verify that it still works when `mypy` is absent. - Reinstall `mypy` and run `python test/test_type_hints.py` to verify that this didn't break the `TestTypeHints` suite. - Run `python test/test_type_hints.py` again (should finish quickly) to verify that this didn't break `mypy` caching. - Run `torch/testing/_internal/mypy_wrapper.py` on a few Python files in this repo to verify that it doesn't give any additional warnings when the `TestTypeHints` suite passes. Some examples (compare with the behavior of just running `mypy` on these files): ```sh torch/testing/_internal/mypy_wrapper.py $PWD/README.md torch/testing/_internal/mypy_wrapper.py $PWD/tools/fast_nvcc/fast_nvcc.py torch/testing/_internal/mypy_wrapper.py $PWD/test/test_type_hints.py torch/testing/_internal/mypy_wrapper.py $PWD/torch/random.py torch/testing/_internal/mypy_wrapper.py $PWD/torch/testing/_internal/mypy_wrapper.py ``` - Remove type hints from `torch.testing._internal.mypy_wrapper` and verify that running `mypy_wrapper.py` on that file gives type errors. - Remove the path to `mypy_wrapper.py` from the `files` setting in `mypy-strict.ini` and verify that running it again on itself no longer gives type errors. - Add `test/test_type_hints.py` to the `files` setting in `mypy-strict.ini` and verify that running the `mypy` wrapper on it again now gives type errors. - Change a return type in `torch/random.py` and verify that running the `mypy` wrapper on it again now gives type errors. - Add the suggested JSON from the docstring of `torch.testing._internal.mypy_wrapper.main` to your `.vscode/settings.json` and verify that VS Code gives the same results (inline, while editing any Python file in the repo) as running the `mypy` wrapper on the command line, in all the above cases. Reviewed By: walterddr Differential Revision: D26049052 Pulled By: samestep fbshipit-source-id: 0b35162fc78976452b5ea20d4ab63937b3c7695d
64 lines
1.8 KiB
INI
64 lines
1.8 KiB
INI
# This is the PyTorch mypy-strict.ini file (note: don't change this line! -
|
|
# test_run_mypy in test/test_type_hints.py uses this string)
|
|
|
|
# Unlike mypy.ini, it enforces very strict typing rules. The intention is for
|
|
# this config file to be used to ENFORCE that people are using mypy on codegen
|
|
# files.
|
|
|
|
# For now, only code_template.py and benchmark utils Timer are covered this way
|
|
|
|
[mypy]
|
|
python_version = 3.6
|
|
|
|
cache_dir = .mypy_cache/strict
|
|
strict_optional = True
|
|
show_column_numbers = True
|
|
warn_no_return = True
|
|
disallow_any_unimported = True
|
|
|
|
# Across versions of mypy, the flags toggled by --strict vary. To ensure
|
|
# we have reproducible type check, we instead manually specify the flags
|
|
warn_unused_configs = True
|
|
disallow_any_generics = True
|
|
disallow_subclassing_any = True
|
|
disallow_untyped_calls = True
|
|
disallow_untyped_defs = True
|
|
disallow_incomplete_defs = True
|
|
check_untyped_defs = True
|
|
disallow_untyped_decorators = True
|
|
no_implicit_optional = True
|
|
warn_redundant_casts = True
|
|
warn_unused_ignores = True
|
|
warn_return_any = True
|
|
implicit_reexport = False
|
|
strict_equality = True
|
|
|
|
files = tools/codegen/gen.py,
|
|
tools/autograd/*.py,
|
|
tools/pyi/*.py,
|
|
torch/testing/_internal/mypy_wrapper.py,
|
|
torch/utils/benchmark/utils/common.py,
|
|
torch/utils/benchmark/utils/timer.py,
|
|
torch/utils/benchmark/utils/valgrind_wrapper/*.py,
|
|
torch/utils/_pytree.py
|
|
|
|
# Specifically enable imports of benchmark utils. As more of `torch` becomes
|
|
# strict compliant, those modules can be enabled as well.
|
|
[mypy-torch.utils.benchmark.utils.*]
|
|
follow_imports = normal
|
|
|
|
# Don't follow imports as much of `torch` is not strict compliant.
|
|
[mypy-torch]
|
|
follow_imports = skip
|
|
|
|
[mypy-torch.*]
|
|
follow_imports = skip
|
|
|
|
# Missing stubs.
|
|
|
|
[mypy-numpy]
|
|
ignore_missing_imports = True
|
|
|
|
[mypy-mypy.*]
|
|
ignore_missing_imports = True
|