Skip to content

Commit 9ea04d4

Browse files
authored
Precommit Autofixing (#9061)
Part of #9058 ### Description This adds black and isort pre-commit hooks. This should fix formatting in the same way as `runtests.sh --autofix` but automatically, and before other actions run for too long in a PR. Note that the versions of both need to be set in the `.pre-commit-config.yaml` file separately from wherever else they're specified, so when versions are changed they need to be synced between files. The version for isort is left unchanged but the `<6` restriction should be removed shortly. The pre-commit step for `pycln` was removed in favour of checking for unused imports with Ruff by enabling F401 in `pyproject.toml`. ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
1 parent 7fe412b commit 9ea04d4

3 files changed

Lines changed: 53 additions & 35 deletions

File tree

.pre-commit-config.yaml

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ repos:
1313
hooks:
1414
- id: end-of-file-fixer
1515
- id: trailing-whitespace
16+
- id: check-ast
1617
- id: check-yaml
1718
- id: check-docstring-first
1819
- id: check-executables-have-shebangs
1920
- id: check-toml
2021
- id: check-case-conflict
22+
- id: check-illegal-windows-names
2123
- id: check-added-large-files
2224
args: ['--maxkb=1024']
2325
- id: detect-private-key
@@ -26,8 +28,9 @@ repos:
2628
args: ['--autofix', '--no-sort-keys', '--indent=4']
2729
- id: end-of-file-fixer
2830
- id: mixed-line-ending
31+
2932
- repo: https://github.com/astral-sh/ruff-pre-commit
30-
rev: v0.15.20
33+
rev: v0.16.5
3134
hooks:
3235
- id: ruff-check
3336
args: ["--fix"]
@@ -37,8 +40,25 @@ repos:
3740
^monai/_version.py
3841
)
3942
40-
- repo: https://github.com/hadialqattan/pycln
41-
rev: v2.6.0
43+
- repo: https://github.com/psf/black-pre-commit-mirror
44+
rev: 26.5.1 # Black version, keep synced with MONAI requirements
4245
hooks:
43-
- id: pycln
44-
args: [--config=pyproject.toml]
46+
- id: black
47+
language_version: python3
48+
# black will be given individual file names and so will ignore the excludes in pyproject.toml
49+
exclude: |
50+
(?x)(
51+
^versioneer.py|
52+
^monai/_version.py
53+
)
54+
55+
- repo: https://github.com/pycqa/isort
56+
rev: 9.0.1 # isort version, keep synced with MONAI requirements
57+
hooks:
58+
- id: isort
59+
name: isort (python)
60+
exclude: |
61+
(?x)(
62+
^versioneer.py|
63+
^monai/_version.py
64+
)

pyproject.toml

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ mlflow = ["mlflow>=3.15.2"]
130130
nibabel = ["nibabel"]
131131
nni = [
132132
"nni; platform_system == 'Linux' and 'arm' not in platform_machine and 'aarch' not in platform_machine",
133-
"filelock<3.12.0" # https://github.com/microsoft/nni/issues/5523
133+
"filelock<3.12.0", # https://github.com/microsoft/nni/issues/5523
134+
"typeguard<3" # https://github.com/microsoft/nni/issues/5457
134135
]
135136
onnx = ["onnx>=1.13.0", "onnxruntime; python_version <= '3.10'", "onnx_graphsurgeon", "onnxscript"]
136137
openslide = ["openslide-python", "openslide-bin"]
@@ -159,9 +160,9 @@ transformers = ["transformers>=5.5.0"] # 5.x needs the transchex BertLayer/Bert
159160
zarr = ["zarr"]
160161
# these dependencies are for testing/building only, they aren't needed for regular use so don't appear in "all"
161162
testing = [
162-
"black>=26.3.1",
163+
"black>=26.5.1",
163164
"coverage>=5.5",
164-
"isort>=5.1,<6,!=6.0.0",
165+
"isort>9.0.0",
165166
"mccabe",
166167
"packaging",
167168
"parameterized",
@@ -170,9 +171,8 @@ testing = [
170171
"pycodestyle",
171172
"pyflakes",
172173
"pyrefly>=1.0.0",
173-
"ruff>=0.14.11,<0.15",
174+
"ruff>=0.16.5",
174175
"tomli", # used in print_dependencies.py for Python<3.11
175-
"typeguard<3", # https://github.com/microsoft/nni/issues/5457
176176
"types-PyYAML",
177177
"types-setuptools"
178178
]
@@ -289,39 +289,35 @@ exclude = '''
289289
)
290290
'''
291291

292-
[tool.pycln]
293-
all = true
294-
exclude = "monai/bundle/__main__.py"
295-
296292
[tool.ruff]
297293
line-length = 120
298294
target-version = "py310"
299295

300296
[tool.ruff.lint]
301297
select = [
302-
"B", # flake8-bugbear - https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
303-
"C90", # mccabe (complexity) - https://docs.astral.sh/ruff/rules/#mccabe-c90
304-
"E", # pycodestyle errors - https://docs.astral.sh/ruff/rules/#error-e
305-
"F", # pyflakes - https://docs.astral.sh/ruff/rules/#pyflakes-f
306-
"N", # pep8-naming - https://docs.astral.sh/ruff/rules/#pep8-naming-n
307-
"PIE", # flake8-pie - https://docs.astral.sh/ruff/rules/#flake8-pie-pie
308-
"TID", # flake8-tidy-imports - https://docs.astral.sh/ruff/rules/#flake8-tidy-imports-tid
309-
"W", # pycodestyle warnings - https://docs.astral.sh/ruff/rules/#warning-w
310-
"NPY", # NumPy specific rules
311-
"UP", # pyupgrade
312-
"RUF100", # aka yesqa
298+
"B", # flake8-bugbear - https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
299+
"C90", # mccabe (complexity) - https://docs.astral.sh/ruff/rules/#mccabe-c90
300+
"E", # pycodestyle errors - https://docs.astral.sh/ruff/rules/#error-e
301+
"F", # pyflakes - https://docs.astral.sh/ruff/rules/#pyflakes-f
302+
"N", # pep8-naming - https://docs.astral.sh/ruff/rules/#pep8-naming-n
303+
"PIE", # flake8-pie - https://docs.astral.sh/ruff/rules/#flake8-pie-pie
304+
"TID", # flake8-tidy-imports - https://docs.astral.sh/ruff/rules/#flake8-tidy-imports-tid
305+
"W", # pycodestyle warnings - https://docs.astral.sh/ruff/rules/#warning-w
306+
"NPY", # NumPy specific rules - https://docs.astral.sh/ruff/rules/#numpy-specific-rules-npy
307+
"UP", # pyupgrade - https://docs.astral.sh/ruff/rules/#pyupgrade-up
308+
"RUF100", # aka yesqa - https://docs.astral.sh/ruff/rules/unused-noqa/
309+
"F401", # unused imports - https://docs.astral.sh/ruff/rules/unused-import/
313310
]
314311
extend-ignore = [
315-
"E741", # ambiguous variable name
316-
"F401", # unused import
312+
"E741", # ambiguous variable name
317313
"NPY002", # numpy-legacy-random
318-
"E203", # whitespace before ':' (pycodestyle)
319-
"E501", # line too long (pycodestyle)
320-
"C408", # unnecessary collection call (flake8-comprehensions)
321-
"N812", # lowercase imported as non lowercase (pep8-naming)
322-
"B023", # function uses loop variable (flake8-bugbear)
323-
"B905", # zip() without an explicit strict= parameter (flake8-bugbear)
324-
"B028", # no explicit stacklevel keyword argument found (flake8-bugbear)
314+
"E203", # whitespace before ':' (pycodestyle)
315+
"E501", # line too long (pycodestyle)
316+
"C408", # unnecessary collection call (flake8-comprehensions)
317+
"N812", # lowercase imported as non lowercase (pep8-naming)
318+
"B023", # function uses loop variable (flake8-bugbear)
319+
"B905", # zip() without an explicit strict= parameter (flake8-bugbear)
320+
"B028", # no explicit stacklevel keyword argument found (flake8-bugbear)
325321
]
326322

327323
[tool.ruff.lint.per-file-ignores]
@@ -334,6 +330,8 @@ extend-ignore = [
334330
"monai/apps/detection/utils/ATSS_matcher.py" = [
335331
"N999"
336332
]
333+
"__init__.py" = ["F401"] # TODO: change importation in __init__.py files to suit F401
334+
"monai/bundle/__main__.py" = ["F401"]
337335

338336
[tool.ruff.lint.mccabe]
339337
max-complexity = 50 # todo lower this treshold when yesqa id replaced with Ruff's RUF100

runtests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ function print_style_fail_msg() {
221221
echo "${red}Check failed!${noColor}"
222222
if [ "$homedir" = "$currentdir" ]
223223
then
224-
echo "Please run auto style fixes: ${green}./runtests.sh --autofix${noColor}"
224+
echo "Please run auto style fixes if necessary: ${green}./runtests.sh --autofix${noColor}"
225225
else :
226226
fi
227227
}

0 commit comments

Comments
 (0)