mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-06 00:20:18 +01:00
Run mypy on more test files (#49658)
Summary: Improves one annotation for `augment_model_with_bundled_inputs` Also add a comment to not work on caffe2 type annotations, that's not worth the effort - those ignores can stay as they are. xref gh-16574 Pull Request resolved: https://github.com/pytorch/pytorch/pull/49658 Reviewed By: heitorschueroff Differential Revision: D25757721 Pulled By: ezyang fbshipit-source-id: 44c396d8da9ef3f41b97f9c46a528f0431c4b463
This commit is contained in:
parent
e35b822d7d
commit
b7bfc723d3
11
mypy.ini
11
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
|
||||
|
||||
|
|
|
|||
|
|
@ -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]),),
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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<suffix>[^\n]*?)"
|
|||
r"(?P<raw>r?)", 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
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user