BLD: switch from isort to ruff in prek config - #32408
Conversation
This drops a job from the hooks which should make them faster. Assisted-by: opencode:claude-sonnet-5
There are a few trivial changes: - drop \ import line continuation - slightly different tie-breaking in mixed from .. import .. and import .. - accept ruff-style line wrapping Still only applies to the galleries.
| MinuteLocator, MonthLocator, RRuleLocator, SecondLocator, | ||
| WeekdayLocator, YearLocator, rrulewrapper) | ||
| from matplotlib.dates import ( | ||
| FR, |
There was a problem hiding this comment.
I went looking for whether we want this long import line broken up now and then said "where is this even used?", and it is because we are using eval later... I feel like we probably shouldn't be advertising/encouraging the usage of eval in our examples, but that also isn't a part of this PR so just noting the thoughts I had when reading this for now and someone can follow up later.
| args: [--fix, --show-fixes] | ||
| # Sort imports in the galleries, mirroring the previous isort scope. | ||
| - id: ruff-check | ||
| name: ruff isort (galleries) |
There was a problem hiding this comment.
Should this be rolled into the ruff-check above?
Thinking that we'd want to add these files to the per-file-ignores/includes lists for the "I" rule, then we wouldn't have to run a separate command.
There was a problem hiding this comment.
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 25cd1b2cff..8b1fea6c68 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -46,14 +46,11 @@ repos:
# Ruff version.
rev: c59bba8fb259db0fec2bbb77ad8ba51ea7341b56 # frozen: v0.15.20
hooks:
- # Run the linter.
+ # Run the linter. Import sorting (I001) is selected repo-wide but
+ # ignored outside the galleries via per-file-ignores in pyproject.toml,
+ # mirroring the previous isort scope.
- id: ruff-check
args: [--fix, --show-fixes]
- # Sort imports in the galleries, mirroring the previous isort scope.
- - id: ruff-check
- name: ruff isort (galleries)
- args: [--fix, --show-fixes, --select=I001]
- files: ^galleries/tutorials/|^galleries/examples/|^galleries/plot_types/
- repo: https://github.com/codespell-project/codespell
rev: 2ccb47ff45ad361a21071a7eedda4c37e6ae8c5a # frozen: v2.4.2
hooks:
diff --git a/pyproject.toml b/pyproject.toml
index b6970faa50..91783c92a0 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -213,6 +213,7 @@ select = [
"E",
"F",
"W",
+ "I001",
"UP035",
# The following error codes require the preview mode to be enabled.
"E111",
@@ -287,6 +288,14 @@ force-sort-within-sections = true
pydata = ["numpy", "matplotlib.pyplot"]
[tool.ruff.lint.per-file-ignores]
+# I001 (import sorting) is only enforced in the galleries (matching the
+# previous isort scope); ignore it everywhere else.
+"ci/**" = ["I001"]
+".circleci/**" = ["I001"]
+"doc/**" = ["I001"]
+"lib/**" = ["I001"]
+"tools/**" = ["I001"]
+"galleries/users_explain/**" = ["I001"]
"*.pyi" = ["E501"]
"*.ipynb" = ["E402"]
"doc/conf.py" = ["E402"]
We can get there, but I'm not sure that it is worth "turn it on and exclude it almost everywhere" to save a second ruff (which is fast) invocation.
There was a problem hiding this comment.
Fair enough, I was assuming we wouldn't need so many exclusions or we'd eventually move on that direction. We can update it to that invocation later if we do go that route.
PR summary
We already are using ruff for most of our linting, switch to it as well for import sorting in the examples.
This caused some minor changes to the imports due to ruff not being a perfect drop-in replacement, but I think it is worth a bit of churn to get down to just one lint/formatting tools.
Applying ruff's version of import sorting to the whole code base hits 357 files which is too much churn to be worth it (we already were not running isort on the main library) and I do have some concern that our internal imports may have some cycles and hence sensitive to the import order (the tests do pass, but still seems like a high-risk choice).
AI Disclosure
I used an LLM to translate from isort's spelling of our configuration to ruff's spelling of the configuration. The first commit is LLM work, the second is running ruff.
PR quality check