Skip to content

flake8 and mypy errors #7200

@mpearce25

Description

@mpearce25

🐛 Describe the bug

I'm planning to contribute type annotations to the library as discussed and tracked in #2025. However, I encountered multiple flake8 and mypy errors on main as of 2cd25c1a05012a3720a87f20cec436811fadeedd. I'd like to fix these before adding additional type annotations. If the proposed fixes below look good I can make a PR.


Running

mypy --config-file mypy.ini yields

torchvision/prototype/datapoints/_datapoint.py:8: error: Module "torch._C" has no attribute "DisableTorchFunctionSubclass"  [attr-defined]
    from torch._C import DisableTorchFunctionSubclass
    ^
torchvision/prototype/transforms/_augment.py:137: error: Unused "type: ignore" comment
            return dict(lam=float(self._dist.sample(())))  # type: ignore[arg-type]
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
torchvision/prototype/transforms/_augment.py:159: error: Unused "type: ignore" comment
            lam = float(self._dist.sample(()))  # type: ignore[arg-type]
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Found 3 errors in 2 files (checked 227 source files)

To address these I would

  • Remove the two extra type ignores
  • Add # type: ignore[attr-defined] after from torch._C import DisableTorchFunctionSubclass

Running flake8 yields

./test/test_ops.py:6:1: F401 'typing.Tuple' imported but unused
./torchvision/models/detection/generalized_rcnn.py:7:1: F401 'typing.Dict' imported but unused
./torchvision/models/detection/generalized_rcnn.py:7:1: F401 'typing.Optional' imported but unused
./torchvision/models/detection/generalized_rcnn.py:7:1: F401 'typing.Union' imported but unused
./torchvision/models/detection/generalized_rcnn.py:10:1: F401 'torch.Tensor' imported but unused
./torchvision/models/detection/roi_heads.py:1:1: F401 'typing.Optional' imported but unused
./torchvision/models/detection/roi_heads.py:1:1: F401 'typing.Tuple' imported but unused
./torchvision/models/detection/roi_heads.py:6:1: F401 'torch.Tensor' imported but unused

To address these I would

  • Remove the unused type imports. They only seemingly get used in docstring type hints. However, removing the import fixes the flake8 errors and doesn't cause issues with ufmt in my testing. example of current docstring type hint that likely led to these imports being added
    # type: (Dict[str, Tensor], List[Dict[str, Tensor]]) -> Union[Dict[str, Tensor], List[Dict[str, Tensor]]]

Versions

Collecting environment information...
PyTorch version: 1.13.1
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 13.1 (x86_64)
GCC version: Could not collect
Clang version: 14.0.0 (clang-1400.0.29.202)
CMake version: Could not collect
Libc version: N/A

Python version: 3.9.16 | packaged by conda-forge | (main, Feb  1 2023, 21:42:20)  [Clang 14.0.6 ] (64-bit runtime)
Python platform: macOS-13.1-x86_64-i386-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz

Versions of relevant libraries:
[pip3] mypy==1.0.0
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.24.2
[pip3] torch==1.13.1
[pip3] torchvision==0.15.0a0+378a327
[conda] mkl                       2022.2.1         h44ed08c_16952    conda-forge
[conda] numpy                     1.24.2           py39h6ee2318_0    conda-forge
[conda] pytorch                   1.13.1          cpu_py39he8d27a1_1    conda-forge
[conda] torchvision               0.15.0a0+378a327           dev_0    <develop>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions