diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2d78b08041..aafc8fd29f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,11 +13,13 @@ repos: hooks: - id: end-of-file-fixer - id: trailing-whitespace + - id: check-ast - id: check-yaml - id: check-docstring-first - id: check-executables-have-shebangs - id: check-toml - id: check-case-conflict + - id: check-illegal-windows-names - id: check-added-large-files args: ['--maxkb=1024'] - id: detect-private-key @@ -26,8 +28,9 @@ repos: args: ['--autofix', '--no-sort-keys', '--indent=4'] - id: end-of-file-fixer - id: mixed-line-ending + - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.15.20 + rev: v0.16.3 hooks: - id: ruff-check args: ["--fix"] @@ -37,8 +40,33 @@ repos: ^monai/_version.py ) - - repo: https://github.com/hadialqattan/pycln - rev: v2.6.0 + - repo: https://github.com/psf/black-pre-commit-mirror + rev: 26.5.1 # Black version, keep synced with MONAI requirements + hooks: + - id: black + language_version: python3 + # black will be given individual file names and so will ignore the excludes in pyproject.toml + exclude: | + (?x)( + ^versioneer.py| + ^monai/_version.py + ) + + - repo: https://github.com/pycqa/isort + rev: 8.0.1 # isort version, keep synced with MONAI requirements + hooks: + - id: isort + name: isort (python) + exclude: | + (?x)( + ^versioneer.py| + ^monai/_version.py + ) + + - repo: https://github.com/facebook/pyrefly-pre-commit + rev: 1.3.0.dev2 # The pyrefly version to use hooks: - - id: pycln - args: [--config=pyproject.toml] + - id: pyrefly-check + name: Pyrefly (type checking) + pass_filenames: false + additional_dependencies: ["numpy>=1.24,<3.0"] # omitted "torch>=2.8.0" to fit into venv pre-commit provides diff --git a/pyproject.toml b/pyproject.toml index 9c5f892283..0591374bc4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -104,6 +104,7 @@ all = [ "torchvision", "tqdm>=4.47.0", "transformers>=4.53.0, <5.0", + "typeguard<3", # https://github.com/microsoft/nni/issues/5457 "zarr" ] clearml = ["clearml>=1.10.0rc0"] @@ -130,7 +131,8 @@ mlflow = ["mlflow>=2.12.2,<3.13"] nibabel = ["nibabel"] nni = [ "nni; platform_system == 'Linux' and 'arm' not in platform_machine and 'aarch' not in platform_machine", - "filelock<3.12.0" # https://github.com/microsoft/nni/issues/5523 + "filelock<3.12.0", # https://github.com/microsoft/nni/issues/5523 + "typeguard<3" # https://github.com/microsoft/nni/issues/5457 ] onnx = ["onnx>=1.13.0", "onnxruntime; python_version <= '3.10'", "onnx_graphsurgeon", "onnxscript"] openslide = ["openslide-python", "openslide-bin"] @@ -161,7 +163,7 @@ zarr = ["zarr"] testing = [ "black>=26.3.1", "coverage>=5.5", - "isort>=5.1,<6,!=6.0.0", + "isort>=5.1,!=6.0.0", "mccabe", "packaging", "parameterized", @@ -170,9 +172,8 @@ testing = [ "pycodestyle", "pyflakes", "pyrefly>=1.0.0", - "ruff>=0.14.11,<0.15", + "ruff>=0.14.11", "tomli", # used in print_dependencies.py for Python<3.11 - "typeguard<3", # https://github.com/microsoft/nni/issues/5457 "types-PyYAML", "types-setuptools" ] @@ -299,29 +300,29 @@ target-version = "py310" [tool.ruff.lint] select = [ - "B", # flake8-bugbear - https://docs.astral.sh/ruff/rules/#flake8-bugbear-b - "C90", # mccabe (complexity) - https://docs.astral.sh/ruff/rules/#mccabe-c90 - "E", # pycodestyle errors - https://docs.astral.sh/ruff/rules/#error-e - "F", # pyflakes - https://docs.astral.sh/ruff/rules/#pyflakes-f - "N", # pep8-naming - https://docs.astral.sh/ruff/rules/#pep8-naming-n - "PIE", # flake8-pie - https://docs.astral.sh/ruff/rules/#flake8-pie-pie - "TID", # flake8-tidy-imports - https://docs.astral.sh/ruff/rules/#flake8-tidy-imports-tid - "W", # pycodestyle warnings - https://docs.astral.sh/ruff/rules/#warning-w - "NPY", # NumPy specific rules - "UP", # pyupgrade - "RUF100", # aka yesqa + "B", # flake8-bugbear - https://docs.astral.sh/ruff/rules/#flake8-bugbear-b + "C90", # mccabe (complexity) - https://docs.astral.sh/ruff/rules/#mccabe-c90 + "E", # pycodestyle errors - https://docs.astral.sh/ruff/rules/#error-e + "F", # pyflakes - https://docs.astral.sh/ruff/rules/#pyflakes-f + "N", # pep8-naming - https://docs.astral.sh/ruff/rules/#pep8-naming-n + "PIE", # flake8-pie - https://docs.astral.sh/ruff/rules/#flake8-pie-pie + "TID", # flake8-tidy-imports - https://docs.astral.sh/ruff/rules/#flake8-tidy-imports-tid + "W", # pycodestyle warnings - https://docs.astral.sh/ruff/rules/#warning-w + "NPY", # NumPy specific rules - https://docs.astral.sh/ruff/rules/#numpy-specific-rules-npy + "UP", # pyupgrade - https://docs.astral.sh/ruff/rules/#pyupgrade-up + "RUF100", # aka yesqa - https://docs.astral.sh/ruff/rules/unused-noqa/ + "F401", # unused imports - https://docs.astral.sh/ruff/rules/unused-import/ ] extend-ignore = [ - "E741", # ambiguous variable name - "F401", # unused import + "E741", # ambiguous variable name "NPY002", # numpy-legacy-random - "E203", # whitespace before ':' (pycodestyle) - "E501", # line too long (pycodestyle) - "C408", # unnecessary collection call (flake8-comprehensions) - "N812", # lowercase imported as non lowercase (pep8-naming) - "B023", # function uses loop variable (flake8-bugbear) - "B905", # zip() without an explicit strict= parameter (flake8-bugbear) - "B028", # no explicit stacklevel keyword argument found (flake8-bugbear) + "E203", # whitespace before ':' (pycodestyle) + "E501", # line too long (pycodestyle) + "C408", # unnecessary collection call (flake8-comprehensions) + "N812", # lowercase imported as non lowercase (pep8-naming) + "B023", # function uses loop variable (flake8-bugbear) + "B905", # zip() without an explicit strict= parameter (flake8-bugbear) + "B028", # no explicit stacklevel keyword argument found (flake8-bugbear) ] [tool.ruff.lint.per-file-ignores] @@ -334,6 +335,8 @@ extend-ignore = [ "monai/apps/detection/utils/ATSS_matcher.py" = [ "N999" ] +"__init__.py" = ["F401"] # TODO: change importation in __init__.py files to suit F401 +"monai/bundle/__main__.py" = ["F401"] [tool.ruff.lint.mccabe] max-complexity = 50 # todo lower this treshold when yesqa id replaced with Ruff's RUF100 diff --git a/runtests.sh b/runtests.sh index 73508a093b..0fc18b36ec 100755 --- a/runtests.sh +++ b/runtests.sh @@ -221,7 +221,7 @@ function print_style_fail_msg() { echo "${red}Check failed!${noColor}" if [ "$homedir" = "$currentdir" ] then - echo "Please run auto style fixes: ${green}./runtests.sh --autofix${noColor}" + echo "Please run auto style fixes if necessary: ${green}./runtests.sh --autofix${noColor}" else : fi }