Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Replace bandit, flake8, isort, pyupgrade, and yesqa with ruff (#2292)

Christian Clauss64bd78719fcd
diff --git a/.flake8 b/.flake8
deleted file mode 100644
index 094739282..000000000
--- a/.flake8
+++ /dev/null
@@ -1,11 +0,0 @@
-[flake8]
-max-line-length = 120
-exclude =
- docs
-per-file-ignores =
- __init__.py: F401, F403
- docs/generate_example_images.py: E402
-verbose = 2
-ignore =
- W503
- E203
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 3f61f556a..b800f088a 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -19,6 +19,12 @@ repos:
hooks:
- id: pyproject-fmt
+ - repo: https://github.com/charliermarsh/ruff-pre-commit
+ rev: v0.0.259
+ hooks:
+ - id: ruff
+ args: [--fix, --exit-non-zero-on-fix]
+
- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
@@ -34,45 +40,8 @@ repos:
- --skip
- "*.bib,*.ipynb"
- - repo: https://github.com/PyCQA/isort
- rev: 5.12.0
- hooks:
- - id: isort
-
- - repo: https://github.com/PyCQA/flake8
- rev: 6.0.0
- hooks:
- - id: flake8
-
- - repo: https://github.com/asottile/pyupgrade
- rev: v3.3.1
- hooks:
- - id: pyupgrade
- args: [--py37-plus]
-
- - repo: https://github.com/PyCQA/bandit
- rev: 1.7.5
- hooks:
- - id: bandit
- name: bandit excluding the test directory
- language_version: python3
- exclude: ^test/
- - id: bandit
- name: bandit only the test directory
- language_version: python3
- files: ^test/
- args:
- - --skip
- - "B101,B311"
-
- repo: https://github.com/PyCQA/docformatter
rev: v1.6.1-rc1
hooks:
- id: docformatter
args: [--in-place, --wrap-summaries=115, --wrap-descriptions=120]
-
- - repo: https://github.com/asottile/yesqa
- rev: v1.4.0
- hooks:
- - id: yesqa
- name: Unused noqa
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 88e1f30e9..87979b8d6 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -288,7 +288,7 @@ test that:
- Typing with [mypy](http://mypy-lang.org) type checks the Python code.
- If the docs can be generated successfully
- [pre-commit ci](https://pre-commit.ci)
- - [flake8](https://pypi.org/project/flake8/) accepts the code style (our guidelines are based on PEP8).
+ - [ruff](https://pypi.org/project/ruff/) accepts the code style (our guidelines are based on PEP8).
- [black](https://black.readthedocs.io/en/stable/) checks if the code is well formatted
- [docformatter](https://pypi.org/project/docformatter/) checks if the code docstrings are well formatted
- and some other checks. Check our [pre-commit config](./.pre-commit-config.yaml)
diff --git a/Makefile b/Makefile
index 3fefb27d8..a93cd9358 100644
--- a/Makefile
+++ b/Makefile
@@ -33,7 +33,7 @@ test-slow: FORCE
pytest -v --device all --dtype all -m "(jit or grad or nn)"
lint: FORCE
- pre-commit run flake8 --all-files
+ pre-commit run ruff --all-files
mypy: FORCE
mypy
diff --git a/kornia/contrib/image_stitching.py b/kornia/contrib/image_stitching.py
index 5a7bae84b..6aef55a52 100644
--- a/kornia/contrib/image_stitching.py
+++ b/kornia/contrib/image_stitching.py
@@ -83,7 +83,7 @@ class ImageStitcher(Module):
def preprocess(self, image_1: Tensor, image_2: Tensor) -> Dict[str, Tensor]:
"""Preprocess input to the required format."""
# TODO: probably perform histogram matching here.
- if isinstance(self.matcher, LoFTR) or isinstance(self.matcher, LocalFeatureMatcher):
+ if isinstance(self.matcher, (LoFTR, LocalFeatureMatcher)):
input_dict = { # LofTR works on grayscale images only
"image0": rgb_to_grayscale(image_1),
"image1": rgb_to_grayscale(image_2),
diff --git a/kornia/geometry/conversions.py b/kornia/geometry/conversions.py
index a315fd2c2..64f9d6a09 100644
--- a/kornia/geometry/conversions.py
+++ b/kornia/geometry/conversions.py
@@ -9,7 +9,6 @@ from kornia.constants import pi
from kornia.core import Tensor, concatenate, pad, stack, tensor, where
from kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_SHAPE
from kornia.utils.helpers import _torch_inverse_cast
-from kornia.utils.misc import eye_like
__all__ = [
"rad2deg",
diff --git a/kornia/geometry/epipolar/fundamental.py b/kornia/geometry/epipolar/fundamental.py
index ad602dc11..5f7101f31 100644
--- a/kornia/geometry/epipolar/fundamental.py
+++ b/kornia/geometry/epipolar/fundamental.py
@@ -89,7 +89,7 @@ def find_fundamental(
raise AssertionError(points1.shape, points2.shape)
if points1.shape[1] < 8:
raise AssertionError(points1.shape)
- if not (weights is None):
+ if weights is not None:
if not (len(weights.shape) == 2 and weights.shape[1] == points1.shape[1]):
raise AssertionError(weights.shape)
diff --git a/kornia/geometry/linalg.py b/kornia/geometry/linalg.py
index c897ad327..aad59f377 100644
--- a/kornia/geometry/linalg.py
+++ b/kornia/geometry/linalg.py
@@ -217,7 +217,7 @@ def point_line_distance(point: Tensor, line: Tensor, eps: float = 1e-9) -> Tenso
KORNIA_CHECK_IS_TENSOR(point)
KORNIA_CHECK_IS_TENSOR(line)
- if not point.shape[-1] in (2, 3):
+ if point.shape[-1] not in (2, 3):
raise ValueError(f"pts must be a (*, 2 or 3) tensor. Got {point.shape}")
if not line.shape[-1] == 3:
diff --git a/kornia/nerf/camera_utils.py b/kornia/nerf/camera_utils.py
index 05283e7e7..72482d881 100755
--- a/kornia/nerf/camera_utils.py
+++ b/kornia/nerf/camera_utils.py
@@ -67,7 +67,7 @@ def parse_colmap_output(
# Read line with camera quaternion
line = line.strip()
- if line.endswith('png') or line.endswith('jpg\n'):
+ if line.endswith(('jpg', 'png')):
split_line = line.split(' ')
qw = float(split_line[1])
qx = float(split_line[2])
diff --git a/pyproject.toml b/pyproject.toml
index ebfdf9b5a..58c63d487 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -76,15 +76,6 @@ namespaces = false
skip-string-normalization = true
skip-magic-trailing-comma = true
-[tool.isort]
- multi_line_output=3
- include_trailing_comma = true
- line_length = 120
- profile = "black"
- known_first_party = ["examples", "kornia","test"]
- skip_glob = []
- skip = ["kornia/__init__.py"]
-
[tool.pytest.ini_options]
addopts = "--color=yes"
testpaths = ["test"]
@@ -129,3 +120,53 @@ D204
D213
D406
D407"""
+
+[tool.ruff]
+select = [
+ "C9",
+ "E",
+ "F",
+ "I",
+ "PL",
+ "S",
+ "UP",
+ "W",
+ "YTT",
+]
+ignore = [
+ "E402",
+ "F403",
+ "PLR0402",
+ "PLR1711",
+ "PLR5501",
+ "PLR2004",
+ "PLW2901"
+]
+line-length = 120
+target-version = "py37"
+
+[tool.ruff.isort]
+ known-first-party = ["examples", "kornia", "test"]
+
+[tool.ruff.mccabe]
+max-complexity = 39
+
+[tool.ruff.pylint]
+max-args = 16
+max-branches = 45
+max-returns = 13
+max-statements = 258
+
+[tool.ruff.per-file-ignores]
+"*/__init__.py" = ["F401"]
+"docs/*" = ["S101", "S113"]
+"docs/generate_example_images.py" = ["C901", "PLR0912", "PLR0915"]
+"examples/train/object_detection/main.py" = ["E741"]
+"kornia/__init__.py" = ["I001"]
+"kornia/augmentation/container/base.py" = ["PLC1901"]
+"kornia/color/hls.py" = ["E741"]
+"kornia/contrib/models/sam/architecture/image_encoder.py" = ["PLR0913"]
+"kornia/enhance/equalization.py" = ["E741"]
+"test/*" = ["PLR0402", "S101"]
+"test/enhance/test_core.py" = ["S311"]
+"test/nerf/test_camera_utils.py" = ["S310"]
diff --git a/requirements/requirements-dev.txt b/requirements/requirements-dev.txt
index d0e7024ca..fce1ffa74 100644
--- a/requirements/requirements-dev.txt
+++ b/requirements/requirements-dev.txt
@@ -1,4 +1,3 @@
-isort
kornia-rs==0.0.8
mypy[reports]
numpy
diff --git a/test/color/test_raw.py b/test/color/test_raw.py
index 04f1f1545..b9ecce761 100644
--- a/test/color/test_raw.py
+++ b/test/color/test_raw.py
@@ -6,8 +6,7 @@ from packaging import version
from torch.autograd import gradcheck
import kornia
-from kornia.testing import BaseTester # test utils
-from kornia.testing import assert_close
+from kornia.testing import BaseTester, assert_close # test utils
class TestRawToRgb(BaseTester):