diff --git a/mypy.ini b/mypy.ini index 7d6161bddd1..bab4ce5dfd4 100644 --- a/mypy.ini +++ b/mypy.ini @@ -17,8 +17,13 @@ check_untyped_defs = True files = torch, caffe2, + test/test_bundled_images.py, + test/test_bundled_inputs.py, test/test_complex.py, + test/test_dataset.py, + test/test_expecttest.py, test/test_futures.py, + test/test_numpy_interop.py, test/test_torch.py, test/test_type_hints.py, test/test_type_info.py @@ -119,6 +124,12 @@ ignore_errors = True [mypy-torch.overrides] ignore_errors = True +# +# 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 diff --git a/test/test_bundled_inputs.py b/test/test_bundled_inputs.py index f57407c9b1d..e12339f3ace 100644 --- a/test/test_bundled_inputs.py +++ b/test/test_bundled_inputs.py @@ -1,5 +1,7 @@ #!/usr/bin/env python3 import io +from typing import List + import torch import torch.utils.bundled_inputs from torch.testing._internal.common_utils import TestCase, run_tests @@ -27,7 +29,7 @@ class TestBundledInputs(TestCase): sm = torch.jit.script(SingleTensorModel()) original_size = model_size(sm) - get_expr = [] + get_expr : List[str] = [] samples = [ # Tensor with small numel and small storage. (torch.tensor([1]),), diff --git a/test/test_expecttest.py b/test/test_expecttest.py index 652a33c4186..5e246179770 100644 --- a/test/test_expecttest.py +++ b/test/test_expecttest.py @@ -4,6 +4,7 @@ import unittest import string import textwrap import doctest +from typing import Dict, Any import hypothesis from hypothesis.strategies import text, integers, composite, sampled_from, booleans @@ -38,7 +39,7 @@ class TestExpectTest(expecttest.TestCase): r3 = {r}{quote}placeholder3{quote} """.format(r='r' if raw else '', quote=quote * 3) new_prog = expecttest.replace_string_literal(textwrap.dedent(prog), 2, t)[0] - ns = {} + ns : Dict[str, Any] = {} exec(new_prog, ns) msg = "program was:\n{}".format(new_prog) self.assertEqual(ns['r'], 'placeholder', msg=msg) # noqa: F821 diff --git a/test/test_numpy_interop.py b/test/test_numpy_interop.py index 35ac4eb9488..81c385ae90a 100644 --- a/test/test_numpy_interop.py +++ b/test/test_numpy_interop.py @@ -47,10 +47,8 @@ class TestNumPyInterop(TestCase): else: # can't directly use min and max, because for int64_t, max - min # is greater than int64_t range and triggers UB. - dtype_info = torch.iinfo(dtype) - low = max(dtype_info.min, int(-1e10)) - high = min(dtype_info.max, int(1e10)) - dtype_info = torch.iinfo(dtype) + low = max(torch.iinfo(dtype).min, int(-1e10)) + high = min(torch.iinfo(dtype).max, int(1e10)) t = torch.empty(shape, dtype=torch.int64).random_(low, high) return t.to(dtype) @@ -272,10 +270,12 @@ class TestNumPyInterop(TestCase): ] for tp, dtype in zip(types, dtypes): if np.dtype(dtype).kind == 'u': - x = torch.Tensor([1, 2, 3, 4]).type(tp) + # .type expects a XxxTensor, which have no type hints on + # purpose, so ignore during mypy type checking + x = torch.Tensor([1, 2, 3, 4]).type(tp) # type: ignore array = np.array([1, 2, 3, 4], dtype=dtype) else: - x = torch.Tensor([1, -2, 3, -4]).type(tp) + x = torch.Tensor([1, -2, 3, -4]).type(tp) # type: ignore array = np.array([1, -2, 3, -4], dtype=dtype) # Test __array__ w/o dtype argument @@ -309,7 +309,7 @@ class TestNumPyInterop(TestCase): float_types = [torch.DoubleTensor, torch.FloatTensor] float_dtypes = [np.float64, np.float32] for tp, dtype in zip(float_types, float_dtypes): - x = torch.Tensor([1, 2, 3, 4]).type(tp) + x = torch.Tensor([1, 2, 3, 4]).type(tp) # type: ignore array = np.array([1, 2, 3, 4], dtype=dtype) for func in ['sin', 'sqrt', 'ceil']: ufunc = getattr(np, func) @@ -321,7 +321,7 @@ class TestNumPyInterop(TestCase): # Test functions with boolean return value for tp, dtype in zip(types, dtypes): - x = torch.Tensor([1, 2, 3, 4]).type(tp) + x = torch.Tensor([1, 2, 3, 4]).type(tp) # type: ignore array = np.array([1, 2, 3, 4], dtype=dtype) geq2_x = np.greater_equal(x, 2) geq2_array = np.greater_equal(array, 2).astype('uint8') @@ -360,7 +360,7 @@ class TestNumPyInterop(TestCase): self.assertEqual(torch.ones([2, 2, 2, 2]).mean(scalar), torch.ones([2, 2, 2, 2]).mean(np_val)) # numpy integral type parses like a python int in custom python bindings: - self.assertEqual(torch.Storage(np_val).size(), scalar) + self.assertEqual(torch.Storage(np_val).size(), scalar) # type: ignore tensor = torch.tensor([2], dtype=torch.int) tensor[0] = np_val diff --git a/torch/testing/_internal/expecttest.py b/torch/testing/_internal/expecttest.py index 9e46a9a84a3..4dae7ebf03d 100644 --- a/torch/testing/_internal/expecttest.py +++ b/torch/testing/_internal/expecttest.py @@ -3,6 +3,7 @@ import unittest import traceback import os import string +from typing import Tuple # This file implements expect tests (also known as "golden" tests). @@ -139,7 +140,8 @@ RE_EXPECT = re.compile(r"^(?P[^\n]*?)" r"(?Pr?)", re.DOTALL) -def replace_string_literal(src, lineno, new_string): +def replace_string_literal(src : str, lineno : int, + new_string : str) -> Tuple[str, int]: r""" Replace a triple quoted string literal with new contents. Only handles printable ASCII correctly at the moment. This diff --git a/torch/utils/bundled_inputs.py b/torch/utils/bundled_inputs.py index c5d603885e4..741c0841778 100644 --- a/torch/utils/bundled_inputs.py +++ b/torch/utils/bundled_inputs.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -from typing import Any, TypeVar, Optional, Tuple, List, NamedTuple, Union +from typing import Any, TypeVar, Optional, Tuple, List, NamedTuple, Union, Sequence import textwrap import torch from torch._C import TupleType, OptionalType, ListType @@ -17,7 +17,7 @@ class InflatableArg(NamedTuple): def augment_model_with_bundled_inputs( model: torch.jit.ScriptModule, - inputs: Optional[List[Tuple[Any, ...]]] = None, + inputs: Optional[Sequence[Tuple[Any, ...]]] = None, _receive_inflate_expr: Optional[List[str]] = None, # For debugging. ) -> None: """Add bundled sample inputs to a model.