From f3cd9f904083166bcd17a64c974332bc429aa1ec Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 5 Apr 2026 18:23:28 +0200 Subject: [PATCH 1/7] Applt ruff/flake8-bugbear rule B007 Loop control variable not used within loop body --- array_api_compat/torch/_aliases.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/array_api_compat/torch/_aliases.py b/array_api_compat/torch/_aliases.py index 88936302..5969e4db 100644 --- a/array_api_compat/torch/_aliases.py +++ b/array_api_compat/torch/_aliases.py @@ -278,7 +278,7 @@ def _axis_none_keepdims(x, ndim, keepdims): # (https://github.com/pytorch/pytorch/issues/71209) # Note that this is only valid for the axis=None case. if keepdims: - for i in range(ndim): + for _ in range(ndim): x = torch.unsqueeze(x, 0) return x From 3c66ce3bc180e110d38f1742d01865b23218340f Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 5 Apr 2026 18:24:05 +0200 Subject: [PATCH 2/7] Apply ruff/flake8-bugbear rule B009 Do not call `getattr` with a constant attribute value. It is not any safer than normal property access. --- tests/test_no_dependencies.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_no_dependencies.py b/tests/test_no_dependencies.py index c53780b2..624f8971 100644 --- a/tests/test_no_dependencies.py +++ b/tests/test_no_dependencies.py @@ -38,11 +38,11 @@ def _test_dependency(mod): assert not is_mod_array(a) assert mod not in sys.modules - is_array_api_obj = getattr(array_api_compat, "is_array_api_obj") + is_array_api_obj = array_api_compat.is_array_api_obj assert is_array_api_obj(a) assert mod not in sys.modules - array_namespace = getattr(array_api_compat, "array_namespace") + array_namespace = array_api_compat.array_namespace array_namespace(Array()) assert mod not in sys.modules From 2a442a594bb17331de9f04fdb0dc5c8454c150ec Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 5 Apr 2026 18:20:43 +0200 Subject: [PATCH 3/7] Apply ruff/flake8-comprehensions rule C401 Unnecessary generator (rewrite as a set comprehension) --- array_api_compat/common/_helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/array_api_compat/common/_helpers.py b/array_api_compat/common/_helpers.py index b43e3d22..c154ad0b 100644 --- a/array_api_compat/common/_helpers.py +++ b/array_api_compat/common/_helpers.py @@ -669,7 +669,7 @@ def your_function(x, y): # torch._dynamo.exc.Unsupported: Dynamo cannot determine whether the underlying object is hashable # Explanation: Dynamo does not know whether the underlying python object for # PythonModuleVariable( Date: Sun, 5 Apr 2026 18:21:33 +0200 Subject: [PATCH 4/7] Apply ruff/flake8-comprehensions rule C414 Unnecessary `list()` call within `sorted()` --- array_api_compat/cupy/linalg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/array_api_compat/cupy/linalg.py b/array_api_compat/cupy/linalg.py index 1943cb15..4e532f9f 100644 --- a/array_api_compat/cupy/linalg.py +++ b/array_api_compat/cupy/linalg.py @@ -56,7 +56,7 @@ __all__ = linalg_all + _linalg.__all__ # cupy 13 does not have __all__, cupy 14 has it: remove duplicates -__all__ = sorted(list(set(__all__))) +__all__ = sorted(set(__all__)) def __dir__() -> list[str]: return __all__ From 7070411487fe556d631921b93c0f44e2c4878266 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 5 Apr 2026 18:33:52 +0200 Subject: [PATCH 5/7] Apply ruff rule RUF013 PEP 484 prohibits implicit `Optional` --- array_api_compat/torch/fft.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/array_api_compat/torch/fft.py b/array_api_compat/torch/fft.py index f11b3eb5..0fa6ea9a 100644 --- a/array_api_compat/torch/fft.py +++ b/array_api_compat/torch/fft.py @@ -17,8 +17,8 @@ def fftn( x: Array, /, *, - s: Sequence[int] = None, - axes: Sequence[int] = None, + s: Sequence[int] | None = None, + axes: Sequence[int] | None = None, norm: Literal["backward", "ortho", "forward"] = "backward", **kwargs: object, ) -> Array: @@ -28,8 +28,8 @@ def ifftn( x: Array, /, *, - s: Sequence[int] = None, - axes: Sequence[int] = None, + s: Sequence[int] | None = None, + axes: Sequence[int] | None = None, norm: Literal["backward", "ortho", "forward"] = "backward", **kwargs: object, ) -> Array: @@ -39,8 +39,8 @@ def rfftn( x: Array, /, *, - s: Sequence[int] = None, - axes: Sequence[int] = None, + s: Sequence[int] | None = None, + axes: Sequence[int] | None = None, norm: Literal["backward", "ortho", "forward"] = "backward", **kwargs: object, ) -> Array: @@ -50,8 +50,8 @@ def irfftn( x: Array, /, *, - s: Sequence[int] = None, - axes: Sequence[int] = None, + s: Sequence[int] | None = None, + axes: Sequence[int] | None = None, norm: Literal["backward", "ortho", "forward"] = "backward", **kwargs: object, ) -> Array: @@ -61,7 +61,7 @@ def fftshift( x: Array, /, *, - axes: int | Sequence[int] = None, + axes: int | Sequence[int] | None = None, **kwargs: object, ) -> Array: return torch.fft.fftshift(x, dim=axes, **kwargs) @@ -70,7 +70,7 @@ def ifftshift( x: Array, /, *, - axes: int | Sequence[int] = None, + axes: int | Sequence[int] | None = None, **kwargs: object, ) -> Array: return torch.fft.ifftshift(x, dim=axes, **kwargs) From 39faf7c2e7d8778ede4827a1984f01e57b763d5f Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 5 Apr 2026 18:48:32 +0200 Subject: [PATCH 6/7] Apply ruff rule RUF100 Unused `noqa` directive --- array_api_compat/__init__.py | 2 +- array_api_compat/numpy/__init__.py | 1 - tests/test_isdtype.py | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/array_api_compat/__init__.py b/array_api_compat/__init__.py index e7480cce..454c7f8b 100644 --- a/array_api_compat/__init__.py +++ b/array_api_compat/__init__.py @@ -19,4 +19,4 @@ """ __version__ = '1.15.0.dev0' -from .common import * # noqa: F401, F403 +from .common import * # noqa: F403 diff --git a/array_api_compat/numpy/__init__.py b/array_api_compat/numpy/__init__.py index bda4356f..973e993d 100644 --- a/array_api_compat/numpy/__init__.py +++ b/array_api_compat/numpy/__init__.py @@ -1,4 +1,3 @@ -# ruff: noqa: PLC0414 from typing import Final from .._internal import clone_module diff --git a/tests/test_isdtype.py b/tests/test_isdtype.py index 6ad45d4c..92e45613 100644 --- a/tests/test_isdtype.py +++ b/tests/test_isdtype.py @@ -61,7 +61,7 @@ def isdtype_(dtype_, kind): res = dtype_categories[kind](dtype_) else: res = dtype_ == kind - assert type(res) is bool # noqa: E721 + assert type(res) is bool return res @pytest.mark.parametrize("library", wrapped_libraries) From 03a46fb98b459f351295e8f3ea8651eef528e6cb Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 5 Apr 2026 18:47:50 +0200 Subject: [PATCH 7/7] Enforce additional ruff rules --- pyproject.toml | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 073074ca..d7339170 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -65,19 +65,29 @@ namespaces = false [tool.ruff.lint] preview = true select = [ -# Defaults -"E4", "E7", "E9", "F", -# Undefined export -"F822", -# Useless import alias -"PLC0414" + # Defaults + "E4", "E7", "E9", "F", + # Additional rules + "B", "C4", "ISC", "PIE", "FLY", "PERF", "UP", "FURB", + # Useless import alias + "PLC0414", + # Unused `noqa` directive + "RUF100", ] ignore = [ # Module import not at top of file "E402", # Do not use bare `except` - "E722" + "E722", + # Use of `functools.cache` on methods can lead to memory leaks + "B019", + # No explicit `stacklevel` keyword argument found + "B028", + # Within an `except` clause, raise exceptions with `raise ... from ...` + "B904", + # `try`-`except` within a loop incurs performance overhead + "PERF203", ]