From 02b9cbea66f28722de93a54a8f1de1b802d445bd Mon Sep 17 00:00:00 2001 From: mpearce25 Date: Fri, 10 Feb 2023 18:58:53 -0500 Subject: [PATCH 1/6] upgrade flake8 version to 6.0.0 --- .pre-commit-config.yaml | 2 +- CONTRIBUTING.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) 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..bf3319af766 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 typing mypy pytest pytest-mock scipy ``` You may also have to install `libpng-dev` and `libjpeg-turbo8-dev` libraries: ```bash @@ -83,7 +83,7 @@ Instead of relying directly on `black` however, we rely on [ufmt](https://github.com/omnilib/ufmt), for compatibility reasons with Facebook internal infrastructure. -To format your code, install `ufmt` with `pip install ufmt==1.3.2 black==22.3.0 usort==1.0.2` and use e.g.: +To format your code, install `ufmt` with `pip install ufmt==1.3.2 black==22.3.0 usort==1.0.2 flake8=6.0.0` and use e.g.: ```bash ufmt format torchvision From c3657e03a1b59cec4b60c7ea20846843c3385773 Mon Sep 17 00:00:00 2001 From: mpearce25 Date: Fri, 10 Feb 2023 19:02:07 -0500 Subject: [PATCH 2/6] fix resulting flake8 errors --- test/test_ops.py | 2 +- torchvision/models/detection/generalized_rcnn.py | 4 ++-- torchvision/models/detection/roi_heads.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) 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..077c00c83ae 100644 --- a/torchvision/models/detection/generalized_rcnn.py +++ b/torchvision/models/detection/generalized_rcnn.py @@ -4,10 +4,10 @@ import warnings from collections import OrderedDict -from typing import Dict, List, Optional, Tuple, Union +from typing import List, Tuple import torch -from torch import nn, Tensor +from torch import nn from ...utils import _log_api_usage_once 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 From 0294c8d798b158a2d3ecfc1037b2ea3c6ed67ea0 Mon Sep 17 00:00:00 2001 From: mpearce25 Date: Fri, 10 Feb 2023 19:26:33 -0500 Subject: [PATCH 3/6] typing generalized _rcnn --- torchvision/models/detection/generalized_rcnn.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/torchvision/models/detection/generalized_rcnn.py b/torchvision/models/detection/generalized_rcnn.py index 077c00c83ae..e9eb64a3430 100644 --- a/torchvision/models/detection/generalized_rcnn.py +++ b/torchvision/models/detection/generalized_rcnn.py @@ -4,10 +4,10 @@ import warnings from collections import OrderedDict -from typing import List, Tuple +from typing import List, Tuple, Union, Dict, Optional import torch -from torch import nn +from torch import nn, Tensor from ...utils import _log_api_usage_once @@ -36,15 +36,13 @@ 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 From cc7e4c515c7690d4e71a48890351c4a80542b90a Mon Sep 17 00:00:00 2001 From: mpearce25 Date: Fri, 10 Feb 2023 19:41:01 -0500 Subject: [PATCH 4/6] format --- torchvision/models/detection/generalized_rcnn.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/torchvision/models/detection/generalized_rcnn.py b/torchvision/models/detection/generalized_rcnn.py index e9eb64a3430..46b9da1a827 100644 --- a/torchvision/models/detection/generalized_rcnn.py +++ b/torchvision/models/detection/generalized_rcnn.py @@ -4,7 +4,7 @@ import warnings from collections import OrderedDict -from typing import List, Tuple, Union, Dict, Optional +from typing import Dict, List, Optional, Tuple, Union import torch from torch import nn, Tensor @@ -36,13 +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: Dict[str, Tensor], detections: 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: List[Tensor], targets: Optional[List[Dict[str, Tensor]]] = None) -> 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 From d1b6a26136e50e93ccffec9a2018b2a33287f476 Mon Sep 17 00:00:00 2001 From: mpearce25 Date: Wed, 22 Feb 2023 21:23:27 -0500 Subject: [PATCH 5/6] Separate flake8 instructions --- CONTRIBUTING.md | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index bf3319af766..860365601bd 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -73,17 +73,16 @@ 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. -To format your code, install `ufmt` with `pip install ufmt==1.3.2 black==22.3.0 usort==1.0.2 flake8=6.0.0` and use e.g.: +To format your code, install `ufmt` with `pip install ufmt==1.3.2 black==22.3.0 usort==1.0.2` and use e.g.: ```bash ufmt format torchvision @@ -97,11 +96,17 @@ 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 +The codebase's pep8 compliance is checked with [flake8](https://flake8.pycqa.org/en/latest/). -##### Pre-commit hooks +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 +122,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 From e0d53abcd4d19efbd7ec0d4bbbacdf8036e75ba3 Mon Sep 17 00:00:00 2001 From: mpearce25 Date: Thu, 23 Feb 2023 21:22:24 -0500 Subject: [PATCH 6/6] rm typing install cmd and format readme --- CONTRIBUTING.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 860365601bd..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 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 @@ -97,6 +97,7 @@ ufmt format `git diff main --name-only` ``` #### Linting + The codebase's pep8 compliance is checked with [flake8](https://flake8.pycqa.org/en/latest/). To check for flake8 errors: