Skip to content

Commit

Permalink
Use Ruff for linting
Browse files Browse the repository at this point in the history
  • Loading branch information
fepegar committed Sep 22, 2024
1 parent 8b4eb31 commit e444058
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 78 deletions.
23 changes: 1 addition & 22 deletions .github/workflows/code_quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
fail-fast: false
matrix:
tox_env:
- lint
- types

steps:
Expand All @@ -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
Expand Down
44 changes: 7 additions & 37 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
5 changes: 2 additions & 3 deletions print_system.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# flake8: noqa
import platform
import re
import sys

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__)
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ dev = [
"black",
"bump2version",
"coverage",
"flake8",
"mypy",
"parameterized",
"pre-commit",
Expand Down
2 changes: 1 addition & 1 deletion src/torchio/transforms/interpolation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# flake8: noqa: B950
# noqa: B950
import enum

import SimpleITK as sitk
Expand Down
8 changes: 4 additions & 4 deletions tests/data/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
16 changes: 6 additions & 10 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit e444058

Please sign in to comment.