From d26d1c62b59384b5414cb8e54a65e49f626ba2e2 Mon Sep 17 00:00:00 2001 From: Philip Meier Date: Fri, 8 May 2020 20:56:39 +0200 Subject: [PATCH 1/9] add mypy config --- mypy.ini | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 mypy.ini diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 00000000000..c0d6fbb0840 --- /dev/null +++ b/mypy.ini @@ -0,0 +1,30 @@ +[mypy] + +files = torchvision +show_error_codes = True +pretty = True + +[mypy-torchvision.datasets.*] + +ignore_errors = True + +[mypy-torchvision.io.*] + +ignore_errors = True + +[mypy-torchvision.models.*] + +ignore_errors = True + +[mypy-torchvision.ops.*] + +ignore_errors = True + +[mypy-torchvision.transforms.*] + +ignore_errors = True + +[mypy-PIL] + +ignore_missing_imports = True + From 4088a90e032d9684afd2ded55869883c916b2611 Mon Sep 17 00:00:00 2001 From: Philip Meier Date: Fri, 8 May 2020 20:57:01 +0200 Subject: [PATCH 2/9] fix syntax error --- torchvision/io/_video_opt.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/torchvision/io/_video_opt.py b/torchvision/io/_video_opt.py index da37c66cfa4..7dfa48e0af6 100644 --- a/torchvision/io/_video_opt.py +++ b/torchvision/io/_video_opt.py @@ -84,7 +84,8 @@ def __init__(self): def _validate_pts(pts_range): - # type: (List[int]) + # type: (List[int]) -> None + if pts_range[1] > 0: assert ( pts_range[0] <= pts_range[1] From e768986295c0f62be9c37ae67a549c1fca53904d Mon Sep 17 00:00:00 2001 From: Philip Meier Date: Fri, 8 May 2020 20:59:27 +0200 Subject: [PATCH 3/9] fix annotations in torchvision/utils.py --- torchvision/utils.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/torchvision/utils.py b/torchvision/utils.py index 9ee3e31c240..be373138c5f 100644 --- a/torchvision/utils.py +++ b/torchvision/utils.py @@ -1,4 +1,4 @@ -from typing import Union, Optional, Sequence, Tuple, Text, BinaryIO +from typing import Union, Optional, List, Tuple, Text, BinaryIO import io import pathlib import torch @@ -7,7 +7,7 @@ def make_grid( - tensor: Union[torch.Tensor, Sequence[torch.Tensor]], + tensor: Union[torch.Tensor, List[torch.Tensor]], nrow: int = 8, padding: int = 2, normalize: bool = False, @@ -91,15 +91,17 @@ def norm_range(t, range): for x in irange(xmaps): if k >= nmaps: break - grid.narrow(1, y * height + padding, height - padding)\ - .narrow(2, x * width + padding, width - padding)\ - .copy_(tensor[k]) + # Tensor.copy_() is a valid method but seems to be missing from the stubs + # https://pytorch.org/docs/stable/tensors.html#torch.Tensor.copy_ + grid.narrow(1, y * height + padding, height - padding).narrow( # type: ignore[attr-defined] + 2, x * width + padding, width - padding + ).copy_(tensor[k]) k = k + 1 return grid def save_image( - tensor: Union[torch.Tensor, Sequence[torch.Tensor]], + tensor: Union[torch.Tensor, List[torch.Tensor]], fp: Union[Text, pathlib.Path, BinaryIO], nrow: int = 8, padding: int = 2, From 9e9d4a2b42593d409b457744354021b3043d4125 Mon Sep 17 00:00:00 2001 From: Philip Meier Date: Fri, 8 May 2020 21:05:06 +0200 Subject: [PATCH 4/9] add mypy type check to CircleCI --- .circleci/config.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index 81b50fffd54..35744194ab0 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -111,6 +111,16 @@ jobs: pip install --user --progress-bar off flake8 typing flake8 . + python_type_check: + docker: + - image: circleci/python:3.7 + steps: + - checkout + - run: + command: | + pip install --user --progress-bar off mypy + mypy + clang_format: docker: - image: circleci/python:3.7 @@ -702,12 +712,14 @@ workflows: python_version: "3.6" cu_version: "cu101" - python_lint + - python_type_check - clang_format nightly: jobs: - circleci_consistency - python_lint + - python_type_check - clang_format - binary_linux_wheel: cu_version: cpu From 384d853180bdd69cf561d35051515b412964f400 Mon Sep 17 00:00:00 2001 From: Philip Meier Date: Fri, 8 May 2020 21:07:49 +0200 Subject: [PATCH 5/9] add mypy cache to ignore files --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 5f483c84327..6bea8609b93 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,4 @@ htmlcov *.swp *.swo gen.yml +.mypy_cache From 4c8fbf48acdb3d033f3b01d6c5ea9da1a6a8fa77 Mon Sep 17 00:00:00 2001 From: Philip Meier Date: Fri, 8 May 2020 21:25:32 +0200 Subject: [PATCH 6/9] try fix CI --- .circleci/config.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 35744194ab0..ea1c534390f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -118,7 +118,9 @@ jobs: - checkout - run: command: | - pip install --user --progress-bar off mypy + pip install --user --progress-bar off numpy mypy + pip install --user --progress-bar off --pre torch -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html + pip install --user --progress-bar off . mypy clang_format: From e12c4d45ba3249fb839f52cf72ae57462e1a961d Mon Sep 17 00:00:00 2001 From: Philip Meier Date: Fri, 8 May 2020 21:26:53 +0200 Subject: [PATCH 7/9] ignore flake8 F821 since it interferes with mypy --- setup.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index 5b77b5fbce3..19f6d24056a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -9,5 +9,5 @@ max-line-length = 120 [flake8] max-line-length = 120 -ignore = F401,E402,F403,W503,W504 +ignore = F401,E402,F403,W503,W504,F821 exclude = venv From 96f92d0657edc1d9bcd1723bb8310ec41f140df5 Mon Sep 17 00:00:00 2001 From: Philip Meier Date: Fri, 8 May 2020 21:32:24 +0200 Subject: [PATCH 8/9] add mypy type check to config generator --- .circleci/config.yml.in | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/.circleci/config.yml.in b/.circleci/config.yml.in index 4138dca50e7..f1f6df0488f 100644 --- a/.circleci/config.yml.in +++ b/.circleci/config.yml.in @@ -111,6 +111,18 @@ jobs: pip install --user --progress-bar off flake8 typing flake8 . + python_type_check: + docker: + - image: circleci/python:3.7 + steps: + - checkout + - run: + command: | + pip install --user --progress-bar off numpy mypy + pip install --user --progress-bar off --pre torch -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html + pip install --user --progress-bar off . + mypy + clang_format: docker: - image: circleci/python:3.7 @@ -398,6 +410,7 @@ workflows: python_version: "3.6" cu_version: "cu101" - python_lint + - python_type_check - clang_format nightly: @@ -405,5 +418,6 @@ workflows: jobs: - circleci_consistency - python_lint + - python_type_check - clang_format {{ workflows(prefix="nightly_", filter_branch="nightly", upload=True) }} From d8adb4036124adc42de22d2c1d30612feb281e16 Mon Sep 17 00:00:00 2001 From: Philip Meier Date: Fri, 8 May 2020 21:39:41 +0200 Subject: [PATCH 9/9] explicitly set config files --- .circleci/config.yml | 4 ++-- .circleci/config.yml.in | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index ea1c534390f..ea4d09a7855 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -109,7 +109,7 @@ jobs: - run: command: | pip install --user --progress-bar off flake8 typing - flake8 . + flake8 --config=setup.cfg . python_type_check: docker: @@ -121,7 +121,7 @@ jobs: pip install --user --progress-bar off numpy mypy pip install --user --progress-bar off --pre torch -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html pip install --user --progress-bar off . - mypy + mypy --config-file mypy.ini clang_format: docker: diff --git a/.circleci/config.yml.in b/.circleci/config.yml.in index f1f6df0488f..6241f9c9c28 100644 --- a/.circleci/config.yml.in +++ b/.circleci/config.yml.in @@ -109,7 +109,7 @@ jobs: - run: command: | pip install --user --progress-bar off flake8 typing - flake8 . + flake8 --config=setup.cfg . python_type_check: docker: @@ -121,7 +121,7 @@ jobs: pip install --user --progress-bar off numpy mypy pip install --user --progress-bar off --pre torch -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html pip install --user --progress-bar off . - mypy + mypy --config-file mypy.ini clang_format: docker: