pytorch/mypy.ini
Edward Z. Yang 9bce208dfb Replace follow_imports = silent with normal (#118414)
This is a lot of files changed! Don't panic! Here's how it works:

* Previously, we set `follow_imports = silent` for our mypy.ini configuration. Per https://mypy.readthedocs.io/en/stable/running_mypy.html#follow-imports, what this does is whenever we have an import to a module which is not listed as a file to be typechecked in mypy, we typecheck it as normal but suppress all errors that occurred in that file.
* When mypy is run inside lintrunner, the list of files is precisely the files covered by the glob in lintrunner.toml, but with files in excludes excluded.
* The top-level directive `# mypy: ignore-errors` instructs mypy to typecheck the file as normal, but ignore all errors.
* Therefore, it should be equivalent to set `follow_imports = normal`, if we put `# mypy: ignore-errors` on all files that were previously excluded from the file list.
* Having done this, we can remove the exclude list from .lintrunner.toml, since excluding a file from typechecking is baked into the files themselves.
* torch/_dynamo and torch/_inductor were previously in the exclude list, because they were covered by MYPYINDUCTOR. It is not OK to mark these as `# mypy: ignore-errors` as this will impede typechecking on the alternate configuration. So they are temporarily being checked twice, but I am suppressing the errors in these files as the configurations are not quite the same. I plan to unify the configurations so this is only a temporary state.
* There were some straggler type errors after these changes somehow, so I fixed them as needed. There weren't that many.

In the future, to start type checking a file, just remove the ignore-errors directive from the top of the file.

The codemod was done with this script authored by GPT-4:

```
import glob

exclude_patterns = [
    ...
]

for pattern in exclude_patterns:
    for filepath in glob.glob(pattern, recursive=True):
        if filepath.endswith('.py'):
            with open(filepath, 'r+') as f:
                content = f.read()
                f.seek(0, 0)
                f.write('# mypy: ignore-errors\n\n' + content)
```

Signed-off-by: Edward Z. Yang <ezyang@meta.com>

Pull Request resolved: https://github.com/pytorch/pytorch/pull/118414
Approved by: https://github.com/thiagocrepaldi, https://github.com/albanD
2024-01-27 02:44:11 +00:00

279 lines
5.5 KiB
INI

# This is the PyTorch mypy.ini file (note: don't change this line! -
# test_run_mypy in test/test_type_hints.py uses this string)
[mypy]
plugins = mypy_plugins/check_mypy_version.py, numpy.typing.mypy_plugin
cache_dir = .mypy_cache/normal
allow_redefinition = True
warn_unused_configs = True
warn_redundant_casts = True
show_error_codes = True
show_column_numbers = True
check_untyped_defs = True
follow_imports = normal
# do not reenable this:
# https://github.com/pytorch/pytorch/pull/60006#issuecomment-866130657
warn_unused_ignores = False
#
# Note: test/ still has syntax errors so can't be added
#
# Typing tests is low priority, but enabling type checking on the
# untyped test functions (using `--check-untyped-defs`) is still
# high-value because it helps test the typing.
#
files =
torch,
caffe2,
test/test_bundled_images.py,
test/test_bundled_inputs.py,
test/test_complex.py,
test/test_datapipe.py,
test/test_futures.py,
test/test_numpy_interop.py,
test/test_torch.py,
test/test_type_hints.py,
test/test_type_info.py
test/test_utils.py
#
# `exclude` is a regex, not a list of paths like `files` (sigh)
#
exclude = torch/include/|torch/csrc/|torch/distributed/elastic/agent/server/api.py|torch/testing/_internal|torch/distributed/fsdp/fully_sharded_data_parallel.py
# Minimum version supported - variable annotations were introduced
# in Python 3.8
python_version = 3.8
#
# Extension modules without stubs.
#
[mypy-torch._C._jit_tree_views]
ignore_missing_imports = True
[mypy-torch.for_onnx.onnx]
ignore_missing_imports = True
[mypy-torch.ao.quantization.experimental.apot_utils]
ignore_missing_imports = True
[mypy-torch.ao.quantization.experimental.quantizer]
ignore_missing_imports = True
[mypy-torch.ao.quantization.experimental.observer]
ignore_missing_imports = True
[mypy-torch.ao.quantization.experimental.APoT_tensor]
ignore_missing_imports = True
[mypy-torch.ao.quantization.experimental.fake_quantize_function]
ignore_missing_imports = True
[mypy-torch.ao.quantization.experimental.fake_quantize]
ignore_missing_imports = True
#
# Files with various errors. Mostly real errors, possibly some false
# positives as well.
#
[mypy-test_torch]
check_untyped_defs = False
# Excluded from mypy due to OpInfos being annoying to type
[mypy-torch.testing._internal.common_methods_invocations.*]
ignore_errors = True
[mypy-torch.testing._internal.hypothesis_utils.*]
ignore_errors = True
[mypy-torch.testing._internal.common_quantization.*]
ignore_errors = True
[mypy-torch.testing._internal.generated.*]
ignore_errors = True
[mypy-torch.testing._internal.distributed.*]
ignore_errors = True
[mypy-torch.nn.modules.pooling]
ignore_errors = True
[mypy-torch.nn.parallel._functions]
ignore_errors = True
[mypy-torch._appdirs]
ignore_errors = True
[mypy-torch.multiprocessing.pool]
ignore_errors = True
[mypy-torch.overrides]
ignore_errors = True
#
# Files with 'type: ignore' comments that are needed if checked with mypy-strict.ini
#
[mypy-tools.render_junit]
warn_unused_ignores = False
[mypy-tools.generate_torch_version]
warn_unused_ignores = False
#
# Adding type annotations to caffe2 is probably not worth the effort
# only work on this if you have a specific reason for it, otherwise
# leave these ignores as they are.
#
[mypy-caffe2.python.*]
ignore_errors = True
[mypy-caffe2.proto.*]
ignore_errors = True
[mypy-caffe2.core.nomnigraph.op_gen]
ignore_errors = True
[mypy-caffe2.distributed.store_ops_test_util]
ignore_errors = True
[mypy-caffe2.experiments.*]
ignore_errors = True
[mypy-caffe2.contrib.*]
ignore_errors = True
[mypy-caffe2.quantization.server.*]
ignore_errors = True
#
# Third party dependencies that don't have types.
#
[mypy-triton.*]
ignore_missing_imports = True
[mypy-tensorflow.*]
ignore_missing_imports = True
[mypy-tensorboard.*]
ignore_missing_imports = True
[mypy-onnx.*]
ignore_missing_imports = True
[mypy-matplotlib.*]
ignore_missing_imports = True
[mypy-numpy.*]
ignore_missing_imports = True
[mypy-sympy]
ignore_missing_imports = True
[mypy-sympy.*]
ignore_missing_imports = True
[mypy-hypothesis.*]
ignore_missing_imports = True
[mypy-tqdm.*]
ignore_missing_imports = True
[mypy-multiprocessing.*]
ignore_missing_imports = True
[mypy-setuptools.*]
ignore_missing_imports = True
[mypy-distutils.*]
ignore_missing_imports = True
[mypy-nvd3.*]
ignore_missing_imports = True
[mypy-future.utils]
ignore_missing_imports = True
[mypy-past.builtins]
ignore_missing_imports = True
[mypy-numba.*]
ignore_missing_imports = True
[mypy-PIL.*]
ignore_missing_imports = True
[mypy-moviepy.*]
ignore_missing_imports = True
[mypy-cv2.*]
ignore_missing_imports = True
[mypy-torchvision.*]
ignore_missing_imports = True
[mypy-pycuda.*]
ignore_missing_imports = True
[mypy-tensorrt.*]
ignore_missing_imports = True
[mypy-tornado.*]
ignore_missing_imports = True
[mypy-pydot.*]
ignore_missing_imports = True
[mypy-networkx.*]
ignore_missing_imports = True
[mypy-scipy.*]
ignore_missing_imports = True
[mypy-IPython.*]
ignore_missing_imports = True
[mypy-google.protobuf.textformat]
ignore_missing_imports = True
[mypy-lmdb.*]
ignore_missing_imports = True
[mypy-mpi4py.*]
ignore_missing_imports = True
[mypy-skimage.*]
ignore_missing_imports = True
[mypy-librosa.*]
ignore_missing_imports = True
[mypy-mypy.*]
ignore_missing_imports = True
[mypy-xml.*]
ignore_missing_imports = True
[mypy-boto3.*]
ignore_missing_imports = True
[mypy-dill.*]
ignore_missing_imports = True
[mypy-usort.*]
ignore_missing_imports = True
[mypy-torch._inductor.*]
ignore_errors = True
[mypy-torch._dynamo.*]
ignore_errors = True