From 82465c75f7e78dd153178dd77d5f1f7adba2619a Mon Sep 17 00:00:00 2001 From: Guillaume Fraux Date: Tue, 5 Nov 2024 11:12:53 +0100 Subject: [PATCH] Switch to ruff for linting & formatting --- pyproject.toml | 19 +++++++---------- .../rascaline-torch/rascaline/torch/_c_lib.py | 2 +- .../rascaline/torch/calculator_base.py | 3 ++- .../rascaline-torch/rascaline/torch/system.py | 2 +- .../rascaline-torch/rascaline/torch/utils.py | 2 +- python/rascaline/rascaline/utils/_backend.py | 1 - .../utils/clebsch_gordan/_coefficients.py | 1 - .../clebsch_gordan/_density_correlations.py | 1 - .../rascaline/utils/clebsch_gordan/_utils.py | 2 -- .../rascaline/utils/splines/splines.py | 1 - python/rascaline/tests/utils/utils.py | 9 +------- tox.ini | 21 +++++++------------ 12 files changed, 21 insertions(+), 43 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index ff3326d3e..f06fe9fe2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -57,18 +57,15 @@ namespaces = false ### ======================================================================== ### -[tool.black] -extend-exclude = """ - /python/rascaline/rascaline/_c_api\\.py -""" -[tool.isort] -profile = "black" -line_length = 88 -indent = 4 -include_trailing_comma = true -lines_after_imports = 2 -known_first_party = ["rascaline", "save_data"] +[tool.ruff.lint] +select = ["E", "F", "B", "I"] +ignore = ["B018", "B904"] + +[tool.ruff.lint.isort] +lines-after-imports = 2 +known-first-party = ["rascaline"] + ### ======================================================================== ### diff --git a/python/rascaline-torch/rascaline/torch/_c_lib.py b/python/rascaline-torch/rascaline/torch/_c_lib.py index e62c4453f..b07c15476 100644 --- a/python/rascaline-torch/rascaline/torch/_c_lib.py +++ b/python/rascaline-torch/rascaline/torch/_c_lib.py @@ -4,9 +4,9 @@ from collections import namedtuple import metatensor.torch -import torch import rascaline +import torch from ._build_versions import BUILD_RASCALINE_VERSION, BUILD_TORCH_VERSION diff --git a/python/rascaline-torch/rascaline/torch/calculator_base.py b/python/rascaline-torch/rascaline/torch/calculator_base.py index 47015d13b..5eb395ca6 100644 --- a/python/rascaline-torch/rascaline/torch/calculator_base.py +++ b/python/rascaline-torch/rascaline/torch/calculator_base.py @@ -1,9 +1,10 @@ from typing import List, Optional, Union -import torch from metatensor.torch import Labels, TensorMap from metatensor.torch.atomistic import NeighborListOptions +import torch + from .system import System diff --git a/python/rascaline-torch/rascaline/torch/system.py b/python/rascaline-torch/rascaline/torch/system.py index f0bba1599..5b9fb317c 100644 --- a/python/rascaline-torch/rascaline/torch/system.py +++ b/python/rascaline-torch/rascaline/torch/system.py @@ -1,11 +1,11 @@ from typing import List, Optional, Sequence, overload import numpy as np -import torch from metatensor.torch.atomistic import System from packaging import version import rascaline +import torch @overload diff --git a/python/rascaline-torch/rascaline/torch/utils.py b/python/rascaline-torch/rascaline/torch/utils.py index a0b08d158..781ad01db 100644 --- a/python/rascaline-torch/rascaline/torch/utils.py +++ b/python/rascaline-torch/rascaline/torch/utils.py @@ -4,9 +4,9 @@ from typing import Any import metatensor.torch -import torch import rascaline.utils +import torch from .calculator_base import CalculatorModule from .system import System diff --git a/python/rascaline/rascaline/utils/_backend.py b/python/rascaline/rascaline/utils/_backend.py index 979a9f998..f289aaf63 100644 --- a/python/rascaline/rascaline/utils/_backend.py +++ b/python/rascaline/rascaline/utils/_backend.py @@ -37,7 +37,6 @@ class TorchTensor: pass class TorchModule: - def __call__(self, *arg, **kwargs): return self.forward(*arg, **kwargs) diff --git a/python/rascaline/rascaline/utils/clebsch_gordan/_coefficients.py b/python/rascaline/rascaline/utils/clebsch_gordan/_coefficients.py index b34bf5eeb..081278a60 100644 --- a/python/rascaline/rascaline/utils/clebsch_gordan/_coefficients.py +++ b/python/rascaline/rascaline/utils/clebsch_gordan/_coefficients.py @@ -191,7 +191,6 @@ def _build_dense_cg_coeff_dict( for o3_lambda in range( max(l1, l2) - min(l1, l2), min(lambda_max, (l1 + l2)) + 1 ): - complex_cg = _dispatch.to( wigners.clebsch_gordan_array(l1, l2, o3_lambda), backend=arrays_backend, diff --git a/python/rascaline/rascaline/utils/clebsch_gordan/_density_correlations.py b/python/rascaline/rascaline/utils/clebsch_gordan/_density_correlations.py index fbabeed6f..5c9888b76 100644 --- a/python/rascaline/rascaline/utils/clebsch_gordan/_density_correlations.py +++ b/python/rascaline/rascaline/utils/clebsch_gordan/_density_correlations.py @@ -175,7 +175,6 @@ def _density_correlations( # Perform iterative CG tensor products new_lambda_names: List[str] = [] for i_correlation in range(self._n_correlations): - # Increment the density property dimension names density = _increment_property_names(density, 1) diff --git a/python/rascaline/rascaline/utils/clebsch_gordan/_utils.py b/python/rascaline/rascaline/utils/clebsch_gordan/_utils.py index b02292d3d..043e9f748 100644 --- a/python/rascaline/rascaline/utils/clebsch_gordan/_utils.py +++ b/python/rascaline/rascaline/utils/clebsch_gordan/_utils.py @@ -112,7 +112,6 @@ def _compute_output_keys( combinations: List[Tuple[int, int]] = [] for key_1_i in range(len(keys_1)): for key_2_i in range(len(keys_2)): - # Get the keys key_1 = keys_1.entry(key_1_i) key_2 = keys_2.entry(key_2_i) @@ -131,7 +130,6 @@ def _compute_output_keys( for o3_lambda in range( abs(o3_lambda_1 - o3_lambda_2), abs(o3_lambda_1 + o3_lambda_2) + 1 ): - # Calculate new sigma o3_sigma = int( o3_sigma_1 diff --git a/python/rascaline/rascaline/utils/splines/splines.py b/python/rascaline/rascaline/utils/splines/splines.py index 96ee74874..ef5f732ec 100644 --- a/python/rascaline/rascaline/utils/splines/splines.py +++ b/python/rascaline/rascaline/utils/splines/splines.py @@ -686,7 +686,6 @@ def integrand( def _radial_integral_custom( self, n: int, ell: int, positions: np.ndarray, derivative: bool = False ) -> np.ndarray: - P_ell = legendre(ell) if derivative: diff --git a/python/rascaline/tests/utils/utils.py b/python/rascaline/tests/utils/utils.py index 7e7396bb2..c8bebd532 100644 --- a/python/rascaline/tests/utils/utils.py +++ b/python/rascaline/tests/utils/utils.py @@ -7,16 +7,9 @@ # Try to import some modules ase = pytest.importorskip("ase") -import ase.io # noqa: E402 +import ase.io # noqa: E402,F811 -try: - import torch # noqa: F401 - - HAS_TORCH = True -except ImportError: - HAS_TORCH = False - try: import metatensor.operations diff --git a/tox.ini b/tox.ini index c4a5c555d..009d867a1 100644 --- a/tox.ini +++ b/tox.ini @@ -141,35 +141,28 @@ commands = [testenv:lint] -description = - lint the Python code with flake8 (code linter), black (code formatter), and isort - (sorting of imports) +description = Run linters and formatter package = skip deps = - black + ruff blackdoc - flake8 - flake8-bugbear - isort commands = - flake8 {[testenv]lint-folders} - black --check --diff {[testenv]lint-folders} + ruff format --diff {[testenv]lint-folders} + ruff check {[testenv]lint-folders} blackdoc --check --diff {[testenv]lint-folders} - isort --check-only --diff {[testenv]lint-folders} [testenv:format] description = Abuse tox to do actual formatting on all files. package = skip deps = - black + ruff blackdoc - isort commands = - black {[testenv]lint-folders} + ruff format {[testenv]lint-folders} + ruff check --fix-only {[testenv]lint-folders} blackdoc {[testenv]lint-folders} - isort {[testenv]lint-folders} [testenv:build-python]