From 69be4e7544b3b4de17be49420270b656f4f70684 Mon Sep 17 00:00:00 2001 From: Mikhail Druzhinin Date: Sat, 30 Sep 2023 08:53:04 +0300 Subject: [PATCH] Fix mypy errors (#1486) * Fix mypy errors * Move linters ci to single test --- .github/workflows/ci.yml | 41 +++++++++---------- .../augmentations/crops/transforms.py | 1 - albumentations/augmentations/functional.py | 4 +- .../augmentations/geometric/functional.py | 4 +- .../augmentations/geometric/transforms.py | 1 - albumentations/augmentations/utils.py | 20 ++++----- albumentations/core/composition.py | 1 - tests/test_transforms.py | 1 - 8 files changed, 35 insertions(+), 38 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6aeefdc82..7dbe66694 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,35 +37,19 @@ jobs: run: pip install torch==2.0.1 torchvision==0.15.2 - name: Install dependencies run: pip install .[tests] - - name: Install linters - run: > - pip install - flake8==5.0.3 - flake8-docstrings==1.6.0 - isort==5.11.5 - mypy==0.991 - types-PyYAML - types-setuptools - types-pkg-resources - name: Cleanup the build directory uses: JesseTG/rm@v1.0.2 with: path: build - name: Run PyTest run: pytest - - name: Run Flake8 - run: flake8 - - name: Run mypy - run: mypy . - - name: Run isort - run: isort --profile black albumentations - check_code_formatting: - name: Check code formatting with Black + check_code_formatting_types: + name: Check code formatting with Black, isort, mypy, flake8 runs-on: ubuntu-latest strategy: matrix: - python-version: [3.8] + python-version: ["3.11"] steps: - name: Checkout uses: actions/checkout@v2 @@ -75,10 +59,25 @@ jobs: python-version: ${{ matrix.python-version }} - name: Update pip run: python -m pip install --upgrade pip - - name: Install Black - run: pip install black==22.6.0 + - name: Install linters + run: > + pip install + flake8==5.0.3 + flake8-docstrings==1.6.0 + isort==5.11.5 + mypy==0.991 + types-PyYAML + types-setuptools + types-pkg-resources + black==22.6.0 - name: Run Black run: black --config=black.toml --check . + - name: Run Flake8 + run: flake8 + - name: Run mypy + run: mypy . + - name: Run isort + run: isort --profile black albumentations check_transforms_docs: name: Check that transforms docs are not outdated diff --git a/albumentations/augmentations/crops/transforms.py b/albumentations/augmentations/crops/transforms.py index 209c59655..c3b3f33bd 100644 --- a/albumentations/augmentations/crops/transforms.py +++ b/albumentations/augmentations/crops/transforms.py @@ -336,7 +336,6 @@ def __init__( always_apply=False, p=1.0, ): - super(RandomResizedCrop, self).__init__( height=height, width=width, interpolation=interpolation, always_apply=always_apply, p=p ) diff --git a/albumentations/augmentations/functional.py b/albumentations/augmentations/functional.py index f0de82c6c..54dd43e2c 100644 --- a/albumentations/augmentations/functional.py +++ b/albumentations/augmentations/functional.py @@ -591,7 +591,7 @@ def add_rain( image = img.copy() - for (rain_drop_x0, rain_drop_y0) in rain_drops: + for rain_drop_x0, rain_drop_y0 in rain_drops: rain_drop_x1 = rain_drop_x0 + slant rain_drop_y1 = rain_drop_y0 + drop_length @@ -698,7 +698,7 @@ def add_sun_flare(img, flare_center_x, flare_center_y, src_radius, src_color, ci overlay = img.copy() output = img.copy() - for (alpha, (x, y), rad3, (r_color, g_color, b_color)) in circles: + for alpha, (x, y), rad3, (r_color, g_color, b_color) in circles: cv2.circle(overlay, (x, y), rad3, (r_color, g_color, b_color), -1) cv2.addWeighted(overlay, alpha, output, 1 - alpha, 0, output) diff --git a/albumentations/augmentations/geometric/functional.py b/albumentations/augmentations/geometric/functional.py index df0dbe3ec..bbb23639f 100644 --- a/albumentations/augmentations/geometric/functional.py +++ b/albumentations/augmentations/geometric/functional.py @@ -1149,7 +1149,9 @@ def optical_distortion( camera_matrix = np.array([[fx, 0, cx], [0, fy, cy], [0, 0, 1]], dtype=np.float32) distortion = np.array([k, k, 0, 0, 0], dtype=np.float32) - map1, map2 = cv2.initUndistortRectifyMap(camera_matrix, distortion, None, None, (width, height), cv2.CV_32FC1) + map1, map2 = cv2.initUndistortRectifyMap( + camera_matrix, distortion, None, None, (width, height), cv2.CV_32FC1 # type: ignore[attr-defined] + ) return cv2.remap(img, map1, map2, interpolation=interpolation, borderMode=border_mode, borderValue=value) diff --git a/albumentations/augmentations/geometric/transforms.py b/albumentations/augmentations/geometric/transforms.py index b796f594a..1395ae11e 100644 --- a/albumentations/augmentations/geometric/transforms.py +++ b/albumentations/augmentations/geometric/transforms.py @@ -1456,7 +1456,6 @@ def apply_to_bbox(self, bbox: BoxInternalType, stepsx: Tuple = (), stepsy: Tuple return bbox_returned def _normalize(self, h, w, xsteps, ysteps): - # compensate for smaller last steps in source image. x_step = w // self.num_steps last_x_step = min(w, ((self.num_steps + 1) * x_step)) - (self.num_steps * x_step) diff --git a/albumentations/augmentations/utils.py b/albumentations/augmentations/utils.py index bc0b7e6ea..80ee2b9b2 100644 --- a/albumentations/augmentations/utils.py +++ b/albumentations/augmentations/utils.py @@ -38,16 +38,16 @@ } NPDTYPE_TO_OPENCV_DTYPE = { - np.uint8: cv2.CV_8U, - np.uint16: cv2.CV_16U, - np.int32: cv2.CV_32S, - np.float32: cv2.CV_32F, - np.float64: cv2.CV_64F, - np.dtype("uint8"): cv2.CV_8U, - np.dtype("uint16"): cv2.CV_16U, - np.dtype("int32"): cv2.CV_32S, - np.dtype("float32"): cv2.CV_32F, - np.dtype("float64"): cv2.CV_64F, + np.uint8: cv2.CV_8U, # type: ignore[attr-defined] + np.uint16: cv2.CV_16U, # type: ignore[attr-defined] + np.int32: cv2.CV_32S, # type: ignore[attr-defined] + np.float32: cv2.CV_32F, # type: ignore[attr-defined] + np.float64: cv2.CV_64F, # type: ignore[attr-defined] + np.dtype("uint8"): cv2.CV_8U, # type: ignore[attr-defined] + np.dtype("uint16"): cv2.CV_16U, # type: ignore[attr-defined] + np.dtype("int32"): cv2.CV_32S, # type: ignore[attr-defined] + np.dtype("float32"): cv2.CV_32F, # type: ignore[attr-defined] + np.dtype("float64"): cv2.CV_64F, # type: ignore[attr-defined] } diff --git a/albumentations/core/composition.py b/albumentations/core/composition.py index dfc5e86d4..653a367a3 100644 --- a/albumentations/core/composition.py +++ b/albumentations/core/composition.py @@ -413,7 +413,6 @@ def __init__( def __call__(self, *args, force_apply: bool = False, **data) -> typing.Dict[str, typing.Any]: if force_apply or random.random() < self.p: - image = data["image"] # Expand mono images to have a single channel diff --git a/tests/test_transforms.py b/tests/test_transforms.py index 7a2b335ed..0e5829455 100644 --- a/tests/test_transforms.py +++ b/tests/test_transforms.py @@ -857,7 +857,6 @@ def test_shift_scale_separate_shift_x_shift_y(image, mask): @pytest.mark.parametrize(["val_uint8"], [[0], [1], [128], [255]]) def test_glass_blur_float_uint8_diff_less_than_two(val_uint8): - x_uint8 = np.zeros((5, 5)).astype(np.uint8) x_uint8[2, 2] = val_uint8