diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6662d7e9b5f..d18fe4b8303 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -19,7 +19,7 @@ repos: - usort == 1.0.2 - repo: https://github.com/PyCQA/flake8 - rev: 5.0.4 + rev: 6.0.0 hooks: - id: flake8 args: [--config=setup.cfg] diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5bca030976d..561f5ea7013 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -49,7 +49,7 @@ python setup.py develop # MACOSX_DEPLOYMENT_TARGET=10.9 CC=clang CXX=clang++ python setup.py develop # for C++ debugging, please use DEBUG=1 # DEBUG=1 python setup.py develop -pip install flake8 typing mypy pytest pytest-mock scipy +pip install mypy pytest pytest-mock scipy ``` You may also have to install `libpng-dev` and `libjpeg-turbo8-dev` libraries: ```bash @@ -73,12 +73,11 @@ If you would like to contribute a new model, please see [here](#New-architecture If you would like to contribute a new dataset, please see [here](#New-dataset). -### Code formatting and typing +### Code formatting and linting #### Formatting -The torchvision code is formatted by [black](https://black.readthedocs.io/en/stable/), -and checked against pep8 compliance with [flake8](https://flake8.pycqa.org/en/latest/). +The torchvision code is formatted by [black](https://black.readthedocs.io/en/stable/). Instead of relying directly on `black` however, we rely on [ufmt](https://github.com/omnilib/ufmt), for compatibility reasons with Facebook internal infrastructure. @@ -97,11 +96,18 @@ files that were edited in your PR with e.g.: ufmt format `git diff main --name-only` ``` -Similarly, you can check for `flake8` errors with `flake8 torchvision`, although -they should be fairly rare considering that most of the errors are automatically -taken care of by `ufmt` already. +#### Linting -##### Pre-commit hooks +The codebase's pep8 compliance is checked with [flake8](https://flake8.pycqa.org/en/latest/). + +To check for flake8 errors: +1. Install `flake8` with `pip install flake8==6.0.0' +2. Run `flake8 torchvision` + +Errors should be fairly rare considering that most are automatically +taken care of by `ufmt`. + +#### Pre-commit hooks For convenience and **purely optionally**, you can rely on [pre-commit hooks](https://pre-commit.com/) which will run both `ufmt` and `flake8` prior to @@ -117,7 +123,7 @@ more and improve your workflow. You'll see for example that `pre-commit run commit anything, and that the `--no-verify` flag can be added to `git commit` to temporarily deactivate the hooks. -#### Type annotations +### Type annotations The codebase has type annotations, please make sure to add type hints if required. We use `mypy` tool for type checking: ```bash diff --git a/test/test_ops.py b/test/test_ops.py index 3f9400257ba..4fd7ad14bb5 100644 --- a/test/test_ops.py +++ b/test/test_ops.py @@ -3,7 +3,7 @@ from abc import ABC, abstractmethod from functools import lru_cache from itertools import product -from typing import Callable, List, Tuple +from typing import Callable, List import numpy as np import pytest diff --git a/torchvision/models/detection/generalized_rcnn.py b/torchvision/models/detection/generalized_rcnn.py index b481265077f..46b9da1a827 100644 --- a/torchvision/models/detection/generalized_rcnn.py +++ b/torchvision/models/detection/generalized_rcnn.py @@ -36,15 +36,17 @@ def __init__(self, backbone: nn.Module, rpn: nn.Module, roi_heads: nn.Module, tr self._has_warned = False @torch.jit.unused - def eager_outputs(self, losses, detections): - # type: (Dict[str, Tensor], List[Dict[str, Tensor]]) -> Union[Dict[str, Tensor], List[Dict[str, Tensor]]] + def eager_outputs( + self, losses: Dict[str, Tensor], detections: List[Dict[str, Tensor]] + ) -> Union[Dict[str, Tensor], List[Dict[str, Tensor]]]: if self.training: return losses return detections - def forward(self, images, targets=None): - # type: (List[Tensor], Optional[List[Dict[str, Tensor]]]) -> Tuple[Dict[str, Tensor], List[Dict[str, Tensor]]] + def forward( + self, images: List[Tensor], targets: Optional[List[Dict[str, Tensor]]] = None + ) -> Tuple[Dict[str, Tensor], List[Dict[str, Tensor]]]: """ Args: images (list[Tensor]): images to be processed diff --git a/torchvision/models/detection/roi_heads.py b/torchvision/models/detection/roi_heads.py index 51b210cb6f3..f6ecd2a8a61 100644 --- a/torchvision/models/detection/roi_heads.py +++ b/torchvision/models/detection/roi_heads.py @@ -1,9 +1,9 @@ -from typing import Dict, List, Optional, Tuple +from typing import Dict, List import torch import torch.nn.functional as F import torchvision -from torch import nn, Tensor +from torch import nn from torchvision.ops import boxes as box_ops, roi_align from . import _utils as det_utils