From 9f8a727fccfd811093d537f2907fae6e586067dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20P=C3=A9rez-Garc=C3=ADa?= Date: Sun, 22 Sep 2024 21:41:47 +0100 Subject: [PATCH] Use Ruff for linting (#1203) --- .github/workflows/code_quality.yml | 23 +------------ .pre-commit-config.yaml | 44 ++++--------------------- print_system.py | 5 ++- pyproject.toml | 1 - src/torchio/transforms/interpolation.py | 2 +- tests/data/test_io.py | 8 ++--- tox.ini | 16 ++++----- 7 files changed, 21 insertions(+), 78 deletions(-) diff --git a/.github/workflows/code_quality.yml b/.github/workflows/code_quality.yml index 9ab2b3c78..8635a95e4 100644 --- a/.github/workflows/code_quality.yml +++ b/.github/workflows/code_quality.yml @@ -17,6 +17,7 @@ jobs: fail-fast: false matrix: tox_env: + - lint - types steps: @@ -38,28 +39,6 @@ jobs: - name: Run check for tox env "${{ matrix.tox_env }}" run: tox -e ${{ matrix.tox_env }} - - flake8: - name: Code style - runs-on: ubuntu-latest - - steps: - - name: Set up Python - uses: actions/setup-python@v5 - with: - python-version: "3.12" - - - name: Checkout repository - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - - name: Install pre-commit - run: pip install pre-commit - - - name: Run flake8 hooks - run: pre-commit run --all-files flake8 - black: name: Code formatting runs-on: ubuntu-latest diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ef0075ccf..5153dbb90 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -6,41 +6,22 @@ repos: hooks: - id: check-ast # Simply check whether the files parse as valid python - id: check-case-conflict # Check for files that would conflict in case-insensitive filesystems - - id: check-builtin-literals # Require literal syntax when initializing empty or zero Python builtin types - - id: check-docstring-first # Checks a common error of defining a docstring after code - id: check-merge-conflict # Check for files that contain merge conflict strings - id: check-toml # Attempts to load all TOML files to verify syntax - - id: check-yaml # Attempts to load all yaml files to verify syntax - - id: check-vcs-permalinks # Ensures that links to vcs websites are permalinks + - id: check-yaml # Attempts to load all YAML files to verify syntax + - id: check-vcs-permalinks # Ensures that links to VCS websites are permalinks - id: debug-statements # Check for debugger imports and py37+ `breakpoint()` calls in python source - id: detect-private-key # Detects the presence of private keys - - id: double-quote-string-fixer # Replaces double quoted strings with single quoted strings - id: end-of-file-fixer # Ensures that a file is either empty, or ends with one newline - id: mixed-line-ending # Replaces or checks mixed line ending - - id: trailing-whitespace # This hook trims trailing whitespace + - id: trailing-whitespace # Trims trailing whitespace exclude: setup.cfg - - repo: https://github.com/psf/black - rev: 24.8.0 + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.6.7 hooks: - - id: black - - - repo: https://github.com/pycqa/flake8 - rev: 7.1.1 - hooks: - - id: flake8 - additional_dependencies: [ - flake8-blind-except, # check for blind, catch-all "except:" statements - flake8-bugbear, # find likely bugs and design problems - flake8-builtins, # check for python builtins being used as variables or parameters - flake8-fixme, # check for FIXME, TODO and other temporary developer notes - flake8-markdown, # lint Python code blocks in Markdown files using flake8 - flake8-pep3101, # check for old string formatting - flake8-print, # check for print statements - flake8-use-fstring, # enforce use of f-string - flake8-2020, # check for misuse of `sys.version` or `sys.version_info` - pep8-naming, # check PEP-8 naming conventions - ] + - id: ruff + # - id: ruff-format - repo: https://github.com/pre-commit/pygrep-hooks rev: v1.10.0 @@ -59,22 +40,11 @@ repos: - id: pyupgrade args: ['--py38-plus', '--keep-runtime-typing'] - - repo: https://github.com/asottile/setup-cfg-fmt - rev: v2.5.0 - hooks: - - id: setup-cfg-fmt - args: ['--include-version-classifiers'] - - repo: https://github.com/PyCQA/doc8 rev: v1.1.2 hooks: - id: doc8 - - repo: https://github.com/PyCQA/docformatter - rev: v1.7.5 - hooks: - - id: docformatter - ci: autoupdate_commit_msg: Autoupdate pre-commit hooks diff --git a/print_system.py b/print_system.py index d643a35d8..089f8432d 100644 --- a/print_system.py +++ b/print_system.py @@ -1,4 +1,3 @@ -# flake8: noqa import platform import re import sys @@ -6,13 +5,13 @@ import numpy import SimpleITK as sitk import torch -import torchio +import torchio as tio sitk_version = re.findall('SimpleITK Version: (.*?)\n', str(sitk.Version()))[0] print('Platform: ', platform.platform()) -print('TorchIO: ', torchio.__version__) +print('TorchIO: ', tio.__version__) print('PyTorch: ', torch.__version__) print('SimpleITK: ', sitk_version) print('NumPy: ', numpy.__version__) diff --git a/pyproject.toml b/pyproject.toml index 3c90ae472..80ad5dc4d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -56,7 +56,6 @@ dev = [ "black", "bump2version", "coverage", - "flake8", "mypy", "parameterized", "pre-commit", diff --git a/src/torchio/transforms/interpolation.py b/src/torchio/transforms/interpolation.py index 34ec4b11f..c3037148f 100644 --- a/src/torchio/transforms/interpolation.py +++ b/src/torchio/transforms/interpolation.py @@ -1,4 +1,4 @@ -# flake8: noqa: B950 +# noqa: B950 import enum import SimpleITK as sitk diff --git a/tests/data/test_io.py b/tests/data/test_io.py index 27131fdb2..0624f00ff 100644 --- a/tests/data/test_io.py +++ b/tests/data/test_io.py @@ -5,8 +5,8 @@ import pytest import SimpleITK as sitk import torch -from torchio.data import io -from torchio.data import ScalarImage + +from torchio.data import ScalarImage, io from ..utils import TorchioTestCase @@ -117,12 +117,12 @@ def test_sitk_to_nib(self): def test_sitk_to_affine(self): spacing = 1, 2, 3 direction_lps = -1, 0, 0, 0, -1, 0, 0, 0, 1 - origin_lps = l, p, s = -10, -20, 30 + origin_lps = left, posterior, superior = -10, -20, 30 image = sitk.GetImageFromArray(np.random.rand(10, 20, 30)) image.SetDirection(direction_lps) image.SetSpacing(spacing) image.SetOrigin(origin_lps) - origin_ras = -l, -p, s + origin_ras = -left, -posterior, superior fixture = np.diag((*spacing, 1)) fixture[:3, 3] = origin_ras affine = io.get_ras_affine_from_sitk(image) diff --git a/tox.ini b/tox.ini index 59ba7a050..486bcb00b 100644 --- a/tox.ini +++ b/tox.ini @@ -17,6 +17,12 @@ commands = --cov-report=xml \ {posargs} +[testenv:lint] +description = Run code linter +skip_install = True +deps = ruff +commands = ruff check + [testenv:types] deps = mypy @@ -26,13 +32,3 @@ commands = --install-types \ --non-interactive \ src - -[flake8] -max-line-length = 88 -select = C,E,F,W,B,B950 -extend-ignore = - N813, - E123, - W503, - E203, - E501