From ba18b0fc8d61ceb95b7f20683463b85f83a58df4 Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Thu, 2 Nov 2023 22:46:55 -0700 Subject: [PATCH] CI: Add `ruff` (#208) This adds `ruff`, a modern version of `pyflakes`, for code linting in pre-commits. --- .pre-commit-config.yaml | 31 ++++++++++++++++++++----------- docs/source/conf.py | 2 +- pyproject.toml | 11 +++++++++++ tests/test_geometry.py | 2 +- tests/test_multifab.py | 2 +- tests/test_particleContainer.py | 1 + 6 files changed, 35 insertions(+), 14 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 60226fa4..5a15511b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -86,17 +86,6 @@ repos: - id: isort name: isort (python) -# Python: Flake8 (checks only, does this support auto-fixes?) -#- repo: https://github.com/PyCQA/flake8 -# rev: 4.0.1 -# hooks: -# - id: flake8 -# additional_dependencies: &flake8_dependencies -# - flake8-bugbear -# - pep8-naming -# exclude: ^(docs/.*|tools/.*)$ -# Alternatively: use autopep8? - # Python Formatting - repo: https://github.com/psf/black rev: 23.10.1 # Keep in sync with blacken-docs @@ -110,6 +99,26 @@ repos: - black==23.10.1 # keep in sync with black hook # TODO: black-jupyter +# Python: Ruff linter +# https://docs.astral.sh/ruff/ +- repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.1.0 + hooks: + - id: ruff + args: [--fix, --exit-non-zero-on-fix] + types_or: [python, jupyter] + +# Python: Flake8 (only for pyflake and bugbear) +# Ruff above is faster +#- repo: https://github.com/PyCQA/flake8 +# rev: 6.1.0 +# hooks: +# - id: flake8 +# additional_dependencies: &flake8_dependencies +# - flake8-bugbear +# - Flake8-pyproject +# exclude: ^(docs/.*|tools/.*)$ + # Jupyter Notebooks: clean up all cell outputs - repo: https://github.com/roy-ht/pre-commit-jupyter rev: v1.2.1 diff --git a/docs/source/conf.py b/docs/source/conf.py index 71ee78ba..606ff3df 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -212,7 +212,7 @@ shutil.rmtree(dst_path) shutil.copytree(src_path, dst_path) -for subdir, dirs, files in os.walk(dst_path): +for subdir, _dirs, files in os.walk(dst_path): for f in files: if f.find(".pyi") > 0: dir_path = os.path.relpath(subdir, dst_path) diff --git a/pyproject.toml b/pyproject.toml index f53522b5..1493420d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,3 +6,14 @@ requires = [ "packaging>=23", ] build-backend = "setuptools.build_meta" + + +[tool.flake8] +#ignore = ['E231', 'E241'] +select = ['F', 'B'] + + +[tool.ruff] +# https://docs.astral.sh/ruff/ +select = ["E4", "E7", "E9", "F"] +ignore = ["E402"] diff --git a/tests/test_geometry.py b/tests/test_geometry.py index 16c51625..4fa77118 100644 --- a/tests/test_geometry.py +++ b/tests/test_geometry.py @@ -132,7 +132,7 @@ def test_periodicity(geometry): gm = geometry bx = gm.Domain() - for non_periodic_coord in [0, 1]: + for _non_periodic_coord in [0, 1]: error_thrown = False try: gm.period(0) diff --git a/tests/test_multifab.py b/tests/test_multifab.py index 3ce66f42..c1bee6a4 100644 --- a/tests/test_multifab.py +++ b/tests/test_multifab.py @@ -147,7 +147,7 @@ def test_mfab_mfiter(mfab): assert iter(mfab).length == 8 cnt = 0 - for mfi in mfab: + for _mfi in mfab: cnt += 1 assert iter(mfab).length == cnt diff --git a/tests/test_particleContainer.py b/tests/test_particleContainer.py index b00725c0..668ed42c 100644 --- a/tests/test_particleContainer.py +++ b/tests/test_particleContainer.py @@ -262,6 +262,7 @@ def test_per_cell(empty_particle_container, std_geometry, std_particle): sum_1 = 0 for tile_ind, pt in lev.items(): + print("tile", tile_ind) real_arrays = pt.GetStructOfArrays().GetRealData() sum_1 += np.sum(real_arrays[1]) print(sum_1)