Repository navigation
pre-commit: format with ruff-unsloth instead of three passes around stock ruff - #14993
Merged
Merged
Conversation
…tock ruff ruff-unsloth (unslothai/ruff, branch unsloth-0.6.9) is ruff 0.6.9 patched to write this repo's style: spaced kwarg and default equals, measured as one column so lines break where stock 0.6.9 breaks them; the def-signature and short-assert magic-comma rules; no blank line after the imports of a short nested block. The hook installs it from the v0.6.9-unsloth.1 release, one wheel per platform by marker, each checked by sha256. run_ruff_format.py now runs ruff-unsloth once and then enforce_kwargs_spacing.py --residual, which keeps only string merging and redundant-pass removal and sends a file to the AST only when a cheap scan says one of them could apply. The pin and version checks read ruff-unsloth. Two files the old hook never formatted are formatted now: the spacing script, which skipped itself, and tests/studio/test_update_release_notes.py, which contains U+2028 and took a path that left it unspaced. Both are AST-identical apart from the new residual code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
ruff-format-with-kwargshook now formats with ruff-unsloth, a ruff 0.6.9 build that writes this repo's style itself, instead of running Python passes before and after stock ruff. Output is unchanged; a full run costs about a quarter of the CPU.Follows up #14174 and #14249.
Why
The old hook parsed every file with Python's
asttwice: once before ruff to normalize magic commas, and once after it to put back the kwarg spacing and import blank lines that ruff had just removed.ast.parsewas about 42 of the hook's CPU seconds;ruff formatitself was about 7. pre-commit.ci runs the hook with--all-fileson every PR, so that cost is paid in full each time.Upstream ruff will not add a kwarg spacing option (astral-sh/ruff#13344), and the formatter is compiled code, so there is nothing to patch at runtime.
ruff-unsloth
Source: https://github.com/unslothai/ruff, branch
unsloth-0.6.9(UNSLOTH.mdlists the rules). Release: https://github.com/unslothai/ruff/releases/tag/v0.6.9-unsloth.1, wheels for Linux x86_64/aarch64/musl, macOS arm64/x86_64 and Windows x86_64, plusSHA256SUMS. It installs as theruff-unslothbinary and theruff_unslothmodule, so it sits next to a stock ruff install.Against ruff 0.6.9 it changes four things:
name = value. The spaced=is measured as one column, so lines break exactly where stock 0.6.9 breaks them, which is what the old post-pass produced.What changes
.pre-commit-config.yaml:additional_dependencieslists one ruff-unsloth wheel per platform, picked by an environment marker and pinned by#sha256=. Musl cannot be told apart from glibc by a marker, so Alpine needs the musllinux wheel installed by hand; platforms with no wheel install nothing and the script refuses with an install hint.scripts/run_ruff_format.py: runspython -m ruff_unsloth formatonce, thenenforce_kwargs_spacing.py --residualin the same bounded chunks as before. The pin is read from the wheel URLs (0.6.9.post1) and compared with the installed distribution's version. Both checks still run before anything is rewritten.scripts/enforce_kwargs_spacing.py: new--residualmode, the two rules ruff-unsloth leaves to Python (merging adjacent same-line literals and dropping redundantpass). A cheap scan decides whether a file could need either; only those files reach the AST, and they get exactly what the full post-pass would write. The default and--premodes are unchanged for running the script on its own.studio-backend-ci.yml: the fixed-point guard installs ruff-unsloth from the same config lines (pip install -r), so pip picks the runner's wheel and checks its hash.scripts/enforce_kwargs_spacing.py, which the old hook skipped because the pass refused to rewrite itself, andtests/studio/test_update_release_notes.py, which contains U+2028 and went down a path that left it unspaced. The second is AST-identical to main; the first differs only in the new residual code.tests/test_enforce_kwargs_spacing_residual.pychecks that every case the full post-pass changes gets the same change, that files nothing applies to never reach the AST, and that every script underscripts/agrees with the full pass.tests/test_run_ruff_format_pin.pyreads the new pin spellings.Results
pre-commit run ruff-format-with-kwargs --all-files, ruff cache cleared before each run, 3 runs per arm on the same host, base and head one after the other:Wall time on this shared 192-core host was noisy (6 to 70 s on head, 16 to 245 s on main), so CPU is the comparable number.
Equivalence
Before switching, the old hook and ruff-unsloth plus the residual pass were run on three rewrites of the whole repo, so that both had real work to do: stock ruff at line length 80 (4,506 files changed), at 140 with magic trailing commas ignored (4,497), and at 100, which strips the kwarg spacing (4,391). The two pipelines produced byte-identical output in all three: 0 files differed. On main itself the only differences are the two files above.
One intended difference: on Python 3.12+ the old pass rewrote f-string debug specifiers,
f"{x=}"tof"{x = }", which changes the printed text. ruff-unsloth leaves f-strings alone. Existing ones in the tree stay as they are.Testing
pre-commit run ruff-format-with-kwargs --all-fileson this branch: Passed, so the tree is a fixed point.tests/test_formatter_fixed_point.pywith ruff-unsloth installed: all pass.workflow-trigger-lint.ymlplustests/security: 3033 passed, 3 failed, and all three fail the same way on unmodified main here (test_lint_exec_literals.py,test_lint_risky_loaders.pyandtest_version_compat_bundle.py::test_the_daily_sweep_skips_what_it_cannot_import).tests/studio/test_update_release_notes.pycould not be run here (structlogis not installed, same on main); its diff is whitespace only.Not verified
additional_dependencies. This PR's own pre-commit.ci run answers it.