Skip to content

pre-commit: format with ruff-unsloth instead of three passes around stock ruff - #14993

Merged
danielhanchen merged 1 commit into
mainfrom
precommit/ruff-unsloth
Oct 24, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
precommit/ruff-unsloth

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

The ruff-format-with-kwargs hook 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 ast twice: 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.parse was about 42 of the hook's CPU seconds; ruff format itself was about 7. pre-commit.ci runs the hook with --all-files on 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.md lists 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, plus SHA256SUMS. It installs as the ruff-unsloth binary and the ruff_unsloth module, so it sits next to a stock ruff install.

Against ruff 0.6.9 it changes four things:

  • Keyword arguments, unannotated defaults (lambdas included) and class pattern keywords are written 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.
  • A def with 3 or more parameters and a default is always one parameter per line; every other def ignores its magic trailing comma.
  • A multi-line assert with no comment and no one-element tuple ignores its magic trailing commas when its rows, joined by single spaces, fit.
  • No blank line after the imports of a nested block of at most 3 statements, when only blank lines separate them from the next statement.

What changes

  • .pre-commit-config.yaml: additional_dependencies lists 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: runs python -m ruff_unsloth format once, then enforce_kwargs_spacing.py --residual in 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 --residual mode, the two rules ruff-unsloth leaves to Python (merging adjacent same-line literals and dropping redundant pass). 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 --pre modes 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.
  • Formatted for the first time: scripts/enforce_kwargs_spacing.py, which the old hook skipped because the pass refused to rewrite itself, and tests/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: tests/test_enforce_kwargs_spacing_residual.py checks 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 under scripts/ agrees with the full pass. tests/test_run_ruff_format_pin.py reads 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:

main this PR
user + sys CPU 82 to 93 s 21 s
result on its own tree Passed Passed

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=}" to f"{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-files on this branch: Passed, so the tree is a fixed point.
  • Formatter tests, including tests/test_formatter_fixed_point.py with ruff-unsloth installed: all pass.
  • The workflow guard suites from workflow-trigger-lint.yml plus tests/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.py and test_version_compat_bundle.py::test_the_daily_sweep_skips_what_it_cannot_import).
  • tests/studio/test_update_release_notes.py could not be run here (structlog is not installed, same on main); its diff is whitespace only.

Not verified

  • That pre-commit.ci installs direct wheel URLs from additional_dependencies. This PR's own pre-commit.ci run answers it.
  • The Linux aarch64, musl and macOS x86_64 wheels were built and smoke-tested only where a native runner exists (Linux x86_64, macOS arm64, Windows); the other three were not run.

…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.
@danielhanchen
danielhanchen merged commit ec6e383 into main Oct 24, 2026
3 of 30 checks passed
@danielhanchen
danielhanchen deleted the precommit/ruff-unsloth branch October 24, 2026 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant