From 872e14004faa5ab160248f3adf8cd49eaceda3b3 Mon Sep 17 00:00:00 2001 From: HAOCHENYE <21724054@zju.edu.cn> Date: Sat, 2 Nov 2024 00:48:24 +0800 Subject: [PATCH] [Fix] Fix pre-commit-hook for python3.12 --- .github/workflows/lint.yml | 6 +++--- .pre-commit-config-zh-cn.yaml | 15 ++++++++++----- .pre-commit-config.yaml | 13 +++++++++---- mmcv/cnn/bricks/drop.py | 3 ++- mmcv/cnn/rfsearch/search.py | 8 ++++---- mmcv/cnn/rfsearch/utils.py | 4 ++-- mmcv/ops/active_rotated_filter.py | 6 +++--- mmcv/ops/cc_attention.py | 2 +- mmcv/ops/chamfer_distance.py | 3 ++- mmcv/ops/correlation.py | 2 +- mmcv/ops/deform_conv.py | 4 +++- mmcv/ops/iou3d.py | 2 +- mmcv/ops/rotated_feature_align.py | 6 +++--- mmcv/ops/scatter_points.py | 2 +- mmcv/ops/sparse_modules.py | 13 ++++++------- mmcv/ops/sparse_structure.py | 2 +- mmcv/ops/tin_shift.py | 8 +++++--- mmcv/ops/upfirdn2d.py | 6 +++--- mmcv/transforms/formatting.py | 1 + mmcv/transforms/processing.py | 2 +- mmcv/transforms/wrappers.py | 2 +- mmcv/video/optflow.py | 4 ++-- mmcv/visualization/image.py | 2 +- 23 files changed, 66 insertions(+), 50 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index ac5afd597a..f670cbd918 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -11,10 +11,10 @@ jobs: runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v2 - - name: Set up Python 3.7 + - name: Set up Python 3.10.15 uses: actions/setup-python@v2 with: - python-version: 3.7 + python-version: '3.10.15' - name: Install pre-commit hook run: | pip install pre-commit @@ -22,7 +22,7 @@ jobs: - name: Linting run: pre-commit run --all-files - name: Format c/cuda codes with clang-format - uses: DoozyX/clang-format-lint-action@v0.11 + uses: DoozyX/clang-format-lint-action@v0.18 with: source: mmcv/ops/csrc extensions: h,c,cpp,hpp,cu,cuh diff --git a/.pre-commit-config-zh-cn.yaml b/.pre-commit-config-zh-cn.yaml index 313c83c84b..f705ec904a 100644 --- a/.pre-commit-config-zh-cn.yaml +++ b/.pre-commit-config-zh-cn.yaml @@ -1,7 +1,11 @@ exclude: ^tests/data/ repos: - - repo: https://gitee.com/openmmlab/mirrors-flake8 - rev: 5.0.4 + - repo: https://github.com/pre-commit/pre-commit + rev: v4.0.0 + hooks: + - id: validate_manifest + - repo: https://github.com/PyCQA/flake8 + rev: 7.1.1 hooks: - id: flake8 - repo: https://gitee.com/openmmlab/mirrors-isort @@ -13,7 +17,7 @@ repos: hooks: - id: yapf - repo: https://gitee.com/openmmlab/mirrors-pre-commit-hooks - rev: v4.3.0 + rev: v5.0.0 hooks: - id: trailing-whitespace - id: check-yaml @@ -53,8 +57,8 @@ repos: hooks: - id: check-copyright args: ["mmcv", "tests", "--excludes", "mmcv/ops"] - - repo: https://gitee.com/openmmlab/mirrors-mypy - rev: v0.812 + - repo: https://github.com/pre-commit/mirrors-mypy + rev: v1.2.0 hooks: - id: mypy exclude: |- @@ -62,6 +66,7 @@ repos: ^test | ^docs ) + additional_dependencies: ["types-setuptools", "types-requests"] # - repo: local # hooks: # - id: clang-format diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 76ded556f9..e3028c5bbe 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,7 +1,11 @@ exclude: ^tests/data/ repos: + - repo: https://github.com/pre-commit/pre-commit + rev: v4.0.0 + hooks: + - id: validate_manifest - repo: https://github.com/PyCQA/flake8 - rev: 5.0.4 + rev: 7.1.1 hooks: - id: flake8 - repo: https://github.com/PyCQA/isort @@ -13,7 +17,7 @@ repos: hooks: - id: yapf - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.3.0 + rev: v5.0.0 hooks: - id: trailing-whitespace - id: check-yaml @@ -39,7 +43,7 @@ repos: - mdformat_frontmatter - linkify-it-py - repo: https://github.com/myint/docformatter - rev: v1.3.1 + rev: 06907d0 hooks: - id: docformatter args: ["--in-place", "--wrap-descriptions", "79"] @@ -54,7 +58,7 @@ repos: - id: check-copyright args: ["mmcv", "tests", "--excludes", "mmcv/ops"] - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.812 + rev: v1.2.0 hooks: - id: mypy exclude: |- @@ -62,6 +66,7 @@ repos: ^test | ^docs ) + additional_dependencies: ["types-setuptools", "types-requests"] # - repo: local # hooks: # - id: clang-format diff --git a/mmcv/cnn/bricks/drop.py b/mmcv/cnn/bricks/drop.py index fe82a25605..c3daaff8d6 100644 --- a/mmcv/cnn/bricks/drop.py +++ b/mmcv/cnn/bricks/drop.py @@ -13,7 +13,8 @@ def drop_path(x: torch.Tensor, residual blocks). We follow the implementation - https://github.com/rwightman/pytorch-image-models/blob/a2727c1bf78ba0d7b5727f5f95e37fb7f8866b1f/timm/models/layers/drop.py # noqa: E501 + https://github.com/rwightman/pytorch-image-models/blob/a2727c1bf78ba0d7b5727f5f95e37fb7f8866b1f/timm/models/layers/drop.py + # noqa: E501 """ if drop_prob == 0. or not training: return x diff --git a/mmcv/cnn/rfsearch/search.py b/mmcv/cnn/rfsearch/search.py index f4add4b23a..9ba5b9dc01 100644 --- a/mmcv/cnn/rfsearch/search.py +++ b/mmcv/cnn/rfsearch/search.py @@ -71,7 +71,7 @@ def __init__(self, self.by_epoch = by_epoch def init_model(self, model: nn.Module): - """init model with search ability. + """Init model with search ability. Args: model (nn.Module): pytorch model @@ -132,7 +132,7 @@ def step(self, model: nn.Module, work_dir: str) -> None: ) def estimate_and_expand(self, model: nn.Module) -> None: - """estimate and search for RFConvOp. + """Estimate and search for RFConvOp. Args: model (nn.Module): pytorch model @@ -146,7 +146,7 @@ def wrap_model(self, model: nn.Module, search_op: str = 'Conv2d', prefix: str = '') -> None: - """wrap model to support searchable conv op. + """Wrap model to support searchable conv op. Args: model (nn.Module): pytorch model @@ -187,7 +187,7 @@ def set_model(self, search_op: str = 'Conv2d', init_rates: Optional[int] = None, prefix: str = '') -> None: - """set model based on config. + """Set model based on config. Args: model (nn.Module): pytorch model diff --git a/mmcv/cnn/rfsearch/utils.py b/mmcv/cnn/rfsearch/utils.py index 4c8168e343..9ced5d040a 100644 --- a/mmcv/cnn/rfsearch/utils.py +++ b/mmcv/cnn/rfsearch/utils.py @@ -4,7 +4,7 @@ def write_to_json(config: dict, filename: str): - """save config to json file. + """Save config to json file. Args: config (dict): Config to be saved. @@ -16,7 +16,7 @@ def write_to_json(config: dict, filename: str): def expand_rates(dilation: tuple, config: dict) -> list: - """expand dilation rate according to config. + """Expand dilation rate according to config. Args: dilation (int): _description_ diff --git a/mmcv/ops/active_rotated_filter.py b/mmcv/ops/active_rotated_filter.py index b8ba43dd41..1ac324126a 100644 --- a/mmcv/ops/active_rotated_filter.py +++ b/mmcv/ops/active_rotated_filter.py @@ -16,9 +16,9 @@ class ActiveRotatedFilterFunction(Function): """Encoding the orientation information and generating orientation- sensitive features. - The details are described in the paper `Align Deep Features for Oriented - Object Detection _`. - """ + The details are described in the paper + `Align Deep Features for Oriented Object Detection _`. + """ # noqa: E501 @staticmethod def forward(ctx, input: torch.Tensor, diff --git a/mmcv/ops/cc_attention.py b/mmcv/ops/cc_attention.py index efde7b703c..4e61357025 100644 --- a/mmcv/ops/cc_attention.py +++ b/mmcv/ops/cc_attention.py @@ -51,7 +51,7 @@ def __init__(self, in_channels: int) -> None: self.in_channels = in_channels def forward(self, x: torch.Tensor) -> torch.Tensor: - """forward function of Criss-Cross Attention. + """Forward function of Criss-Cross Attention. Args: x (torch.Tensor): Input feature with the shape of diff --git a/mmcv/ops/chamfer_distance.py b/mmcv/ops/chamfer_distance.py index d95bd47747..759ef98e90 100644 --- a/mmcv/ops/chamfer_distance.py +++ b/mmcv/ops/chamfer_distance.py @@ -16,7 +16,8 @@ class ChamferDistanceFunction(Function): """This is an implementation of the 2D Chamfer Distance. It has been used in the paper `Oriented RepPoints for Aerial Object - Detection (CVPR 2022) _`. + Detection (CVPR 2022) + _`. """ @staticmethod diff --git a/mmcv/ops/correlation.py b/mmcv/ops/correlation.py index 319b764678..9524610d4f 100644 --- a/mmcv/ops/correlation.py +++ b/mmcv/ops/correlation.py @@ -116,7 +116,7 @@ def _output_size(ctx, input1): class Correlation(nn.Module): - r"""Correlation operator + r"""Correlation operator. This correlation operator works for optical flow correlation computation. diff --git a/mmcv/ops/deform_conv.py b/mmcv/ops/deform_conv.py index c6cbba6779..1bde04d735 100644 --- a/mmcv/ops/deform_conv.py +++ b/mmcv/ops/deform_conv.py @@ -449,7 +449,9 @@ def _load_from_state_dict(self, state_dict, prefix, local_metadata, strict, @MODELS.register_module('DCN', force=True) class DeformConv2dPack_MLU(DeformConv2d): - """This class is the DCN implementation of the MLU device. The MLU + """This class is the DCN implementation of the MLU device. + + The MLU backend support of the operator has been implemented in torchvision. The mmcv registration mechanism is used for multiplexing here. The torchvision implementation of DCN is called. diff --git a/mmcv/ops/iou3d.py b/mmcv/ops/iou3d.py index 94e2057ad2..dd47993917 100755 --- a/mmcv/ops/iou3d.py +++ b/mmcv/ops/iou3d.py @@ -42,7 +42,7 @@ def boxes_iou3d(boxes_a: Tensor, boxes_b: Tensor) -> Tensor: Returns: torch.Tensor: 3D IoU result with shape (M, N). """ - assert boxes_a.shape[1] == boxes_b.shape[1] == 7,\ + assert boxes_a.shape[1] == boxes_b.shape[1] == 7, \ 'Input boxes shape should be (N, 7)' boxes_a_height_max = (boxes_a[:, 2] + boxes_a[:, 5] / 2).view(-1, 1) diff --git a/mmcv/ops/rotated_feature_align.py b/mmcv/ops/rotated_feature_align.py index 0132c04862..893b029443 100644 --- a/mmcv/ops/rotated_feature_align.py +++ b/mmcv/ops/rotated_feature_align.py @@ -17,9 +17,9 @@ class RotatedFeatureAlignFunction(Function): correspond to the refined rotate anchors and reconstruct the feature maps in pixel-wise manner to achieve feature alignment. - The details are described in the paper - `R3Det: Refined Single-Stage Detector with Feature Refinement for Rotating - Object `_. + The details are described in the paper `R3Det: Refined Single-Stage + Detector with Feature Refinement for Rotating Object + `_. """ @staticmethod diff --git a/mmcv/ops/scatter_points.py b/mmcv/ops/scatter_points.py index 5d881bfe63..7b03e8f1da 100644 --- a/mmcv/ops/scatter_points.py +++ b/mmcv/ops/scatter_points.py @@ -20,7 +20,7 @@ def forward(ctx: Any, feats: torch.Tensor, coors: torch.Tensor, reduce_type: str = 'max') -> Tuple[torch.Tensor, torch.Tensor]: - """convert kitti points(N, >=3) to voxels. + """Convert kitti points(N, >=3) to voxels. Args: feats (torch.Tensor): [N, C]. Points features to be reduced diff --git a/mmcv/ops/sparse_modules.py b/mmcv/ops/sparse_modules.py index 20a92aa279..ed2935343b 100644 --- a/mmcv/ops/sparse_modules.py +++ b/mmcv/ops/sparse_modules.py @@ -47,16 +47,15 @@ def _mean_update(vals: Union[int, List], m_vals: Union[int, List], class SparseModule(nn.Module): - """place holder, All module subclass from this will take sptensor in + """Place holder, All module subclass from this will take sptensor in SparseSequential.""" pass class SparseSequential(SparseModule): - r"""A sequential container. - Modules will be added to it in the order they are passed in the - constructor. - Alternatively, an ordered dict of modules can also be passed in. + r"""A sequential container. Modules will be added to it in the order they + are passed in the constructor. Alternatively, an ordered dict of modules + can also be passed in. To make it easier to understand, given is a small example:: @@ -189,14 +188,14 @@ def fused(self): class ToDense(SparseModule): - """convert SparseConvTensor to NCHW dense tensor.""" + """Convert SparseConvTensor to NCHW dense tensor.""" def forward(self, x: SparseConvTensor): return x.dense() class RemoveGrid(SparseModule): - """remove pre-allocated grid buffer.""" + """Remove pre-allocated grid buffer.""" def forward(self, x: SparseConvTensor): x.grid = None diff --git a/mmcv/ops/sparse_structure.py b/mmcv/ops/sparse_structure.py index 83907ab556..9d7cb6e1ff 100644 --- a/mmcv/ops/sparse_structure.py +++ b/mmcv/ops/sparse_structure.py @@ -6,7 +6,7 @@ def scatter_nd(indices: torch.Tensor, updates: torch.Tensor, shape: torch.Tensor) -> torch.Tensor: - """pytorch edition of tensorflow scatter_nd. + """Pytorch edition of tensorflow scatter_nd. this function don't contain except handle code. so use this carefully when indice repeats, don't support repeat add which is supported in tensorflow. diff --git a/mmcv/ops/tin_shift.py b/mmcv/ops/tin_shift.py index 473231cc0d..7d2df2e77a 100755 --- a/mmcv/ops/tin_shift.py +++ b/mmcv/ops/tin_shift.py @@ -55,10 +55,12 @@ class TINShift(nn.Module): Temporal Interlace shift is a differentiable temporal-wise frame shifting which is proposed in "Temporal Interlacing Network" - Please refer to `Temporal Interlacing Network - `_ for more details. + Please refer to + `Temporal Interlacing Network `_ + for more details. - Code is modified from https://github.com/mit-han-lab/temporal-shift-module + Code is modified from + https://github.com/mit-han-lab/temporal-shift-module """ def forward(self, input, shift): diff --git a/mmcv/ops/upfirdn2d.py b/mmcv/ops/upfirdn2d.py index 857e840c1b..e015095033 100644 --- a/mmcv/ops/upfirdn2d.py +++ b/mmcv/ops/upfirdn2d.py @@ -19,7 +19,7 @@ def _parse_scaling(scaling): - """parse scaling into list [x, y]""" + """Parse scaling into list [x, y]""" if isinstance(scaling, int): scaling = [scaling, scaling] assert isinstance(scaling, (list, tuple)) @@ -30,7 +30,7 @@ def _parse_scaling(scaling): def _parse_padding(padding): - """parse padding into list [padx0, padx1, pady0, pady1]""" + """Parse padding into list [padx0, padx1, pady0, pady1]""" if isinstance(padding, int): padding = [padding, padding] assert isinstance(padding, (list, tuple)) @@ -43,7 +43,7 @@ def _parse_padding(padding): def _get_filter_size(filter): - """get width and height of filter kernel.""" + """Get width and height of filter kernel.""" if filter is None: return 1, 1 assert isinstance(filter, torch.Tensor) and filter.ndim in [1, 2] diff --git a/mmcv/transforms/formatting.py b/mmcv/transforms/formatting.py index 02089215e1..89d9d096bb 100644 --- a/mmcv/transforms/formatting.py +++ b/mmcv/transforms/formatting.py @@ -110,6 +110,7 @@ def __init__(self, keys: dict) -> None: def transform(self, results: dict) -> dict: """Transform function to convert image in results to :obj:`torch.Tensor` and transpose the channel order. + Args: results (dict): Result dict contains the image data to convert. Returns: diff --git a/mmcv/transforms/processing.py b/mmcv/transforms/processing.py index 8b65b112b6..d490745a65 100644 --- a/mmcv/transforms/processing.py +++ b/mmcv/transforms/processing.py @@ -1457,7 +1457,7 @@ class RandomResize(BaseTransform): def __init__( self, scale: Union[Tuple[int, int], Sequence[Tuple[int, int]]], - ratio_range: Tuple[float, float] = None, + ratio_range: Optional[Tuple[float, float]] = None, resize_type: str = 'Resize', **resize_kwargs, ) -> None: diff --git a/mmcv/transforms/wrappers.py b/mmcv/transforms/wrappers.py index 27e07d6335..ed20449d1e 100644 --- a/mmcv/transforms/wrappers.py +++ b/mmcv/transforms/wrappers.py @@ -177,7 +177,7 @@ class KeyMapper(BaseTransform): """ def __init__(self, - transforms: Union[Transform, List[Transform]] = None, + transforms: Union[Transform, List[Transform], None] = None, mapping: Optional[Dict] = None, remapping: Optional[Dict] = None, auto_remap: Optional[bool] = None, diff --git a/mmcv/video/optflow.py b/mmcv/video/optflow.py index edd3e42069..4101b6c8f5 100644 --- a/mmcv/video/optflow.py +++ b/mmcv/video/optflow.py @@ -149,8 +149,8 @@ def dequantize_flow(dx: np.ndarray, dx, dy = (dequantize(d, -max_val, max_val, 255) for d in [dx, dy]) if denorm: - dx *= dx.shape[1] - dy *= dx.shape[0] + dx *= dx.shape[1] # type: ignore + dy *= dx.shape[0] # type: ignore flow = np.dstack((dx, dy)) return flow diff --git a/mmcv/visualization/image.py b/mmcv/visualization/image.py index e7ac4c1817..0c4624aabd 100644 --- a/mmcv/visualization/image.py +++ b/mmcv/visualization/image.py @@ -91,7 +91,7 @@ def imshow_bboxes(img: Union[str, np.ndarray], def imshow_det_bboxes(img: Union[str, np.ndarray], bboxes: np.ndarray, labels: np.ndarray, - class_names: List[str] = None, + class_names: Optional[List[str]] = None, score_thr: float = 0, bbox_color: ColorType = 'green', text_color: ColorType = 'green',