Skip to content

[Misc] Apply ruff-format to Python sources and clear ruff check debt - #4008

Open
Aionw wants to merge 5 commits into
kvcache-ai:mainfrom
Aionw:chore/ruff-format-python
Open

Aionw wants to merge 5 commits into
kvcache-ai:mainfrom
Aionw:chore/ruff-format-python

Conversation

@Aionw

@Aionw Aionw commented Sep 10, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Clears the repository-wide Python formatting and lint debt so the existing strict, whole-file gates can stay in place.

On main (430c6e74):

  • 69 of 265 non-vendored Python files were not ruff format-formatted.
  • ruff check reported 40 pre-existing findings across 18 files, 31 of them in the files above.

Both the python-lint CI step and the ruff-format pre-commit hook operate on whole files, so any PR touching one of those files failed on hunks it never wrote (mooncake-wheel/mooncake/structured_object_store.py alone carries 96 pre-existing format hunks), while pre-commit run --files <file> rewrote hundreds of unrelated lines. That is the situation #3567 is working around with a bespoke changed-lines checker; once this lands, the workaround is unnecessary and #3567 can go back to ruff format --check.

This PR deliberately does not change CI policy and does not relax any check — local pre-commit and CI keep enforcing whole files, and they now agree.

Commits

  1. 46749bef — [Misc] Apply ruff-format to Python sources (69 files, +3431/−2139)

    Pure ruff format 0.6.9 output, i.e. the version pinned in pyproject.toml, .pre-commit-config.yaml and CI. The only content-level changes are Ruff's docstring normalizations (trailing whitespace inside docstrings, docstring indentation); string constants, comments and ruff check results are otherwise identical. This commit is listed in .git-blame-ignore-revs.

  2. 4d88638c — [Misc] Ignore the ruff-format commit in git blame

    Adds .git-blame-ignore-revs plus a short note in CONTRIBUTING.md explaining how to enable it locally.

  3. 1826d3ed — [Misc] Clear pre-existing ruff check findings (18 files, +41/−45)

    Required, because 31 of the 40 findings were in files touched by commit 1, so the lint step would otherwise still fail for this PR itself. 23 are ruff check --fix autofixes; the rest were reviewed by hand (details in the commit message and below):

    • import mooncake.pg in mooncake-ep/benchmarks/legacy_buffer_perf.py is kept with # noqa: F401: the import registers the process-group backend, so removing it would change behavior. A pre-existing precedent exists in mooncake-ep/benchmarks/ep_benchmark/run_ep_benchmark.py.
    • The availability probes in mooncake-wheel/tests/test_spdk_tgt_create.py and scripts/test_engram_store.py (import after sys.path setup) keep their imports with # noqa.
    • Removed the shadowed duplicate TestClass.serialize_into; the surviving no-argument definition is the one Python already bound, so behavior is unchanged.
    • Moved module-level imports in mooncake-rl/examples/rl_samples.py and mooncake-wheel/tests/test_put_get_tensor.py into the import block, placed the scripts/test_drain_http_api.py docstring above from __future__ import annotations, narrowed a bare except to (TypeError, ValueError), and renamed an ambiguous l loop variable.

Review tip: commit 1 is mechanical — git show --stat 46749bef is enough; the only commit that needs reading is 1826d3ed.

Module

  • CI/CD
  • Other

Type of Change

  • Other (mechanical formatting + lint cleanup, no functional change)

How Has This Been Tested?

Test commands:

files=$(git ls-files '*.py' | grep -v -e '^extern/' -e '^FAST25-release/')
ruff check "$files"            # All checks passed!            (was: 40 findings)
ruff format --check "$files"   # 265 files already formatted   (was: 69 unformatted)

# exactly what the python-lint CI job runs for this PR (70 files)
changed=$(git diff --name-only --diff-filter=ACMR origin/main...HEAD -- '*.py')
ruff check "$changed"
ruff format --check "$changed"

python -m compileall -q $(git diff --name-only origin/main...HEAD -- '*.py')
prek run trailing-whitespace end-of-file-fixer ruff ruff-format codespell --files $(git diff --name-only origin/main...HEAD)

Test results:

  • ruff check / ruff format --check pass repo-wide and for the PR diff (equivalent of the python-lint CI job)
  • python -m compileall passes for every changed file
  • Repository pre-commit hooks (trailing-whitespace, end-of-file-fixer, ruff, ruff-format, codespell) pass on the changed files
  • AST audit of all 69 reformatted files: the ordered sequence of string constants and the multiset of comments are unchanged; the only differences are Ruff's docstring whitespace/indentation normalization. Neither __doc__ nor inspect.getdoc is used anywhere in the repository, and the only file containing a doctest (scripts/ascend/perf/hccl_tools.py) has an unchanged docstring.
  • .git-blame-ignore-revs verified: git blame falls back to the original authors for reformatted files
  • Python unit/integration tests not run locally (no built wheel in this environment) — relying on CI

Checklist

  • I have performed a self-review of my own code
  • I have formatted my code using ./scripts/code_format.sh (not applicable: the script covers C/C++; Python is formatted with ruff format)
  • I have run pre-commit on the files changed in this PR and all hooks pass
  • I have updated the documentation (if applicable) — CONTRIBUTING.md note for .git-blame-ignore-revs
  • I have added tests to prove my changes are effective (not applicable: formatting/lint-only change)
  • For changes >500 LOC: I have filed an RFC issue (not applicable: purely mechanical formatting with no design impact)

AI Assistance Disclosure

  • AI tools were used — an AI coding agent assisted with the mechanical reformat, the lint fixes, and the verification listed above; the human submitter reviewed every changed line.

Formatting-only cleanup produced with ruff 0.6.9, the version pinned in
pyproject.toml, .pre-commit-config.yaml and CI.

69 of 265 non-vendored Python files were not Ruff-formatted. Because the
CI gate and the `ruff-format` pre-commit hook both work on whole files,
any change touching one of them stalled: CI failed on hundreds of
pre-existing hunks, and `pre-commit run --files <file>` rewrote unrelated
lines. Clearing the debt once lets the existing strict check stay in
place, with local hooks and CI agreeing again.

No logic changes. The only content-level edits are Ruff's docstring
normalizations (trailing whitespace stripped inside docstrings, docstring
indentation normalized); string constants, comments and `ruff check`
findings are otherwise unchanged.
Add `.git-blame-ignore-revs` with the mechanical reformat commit and
document how to enable it, so blame keeps pointing at the real authors
instead of the whole-file formatting change.
`ruff check` reported 40 findings across 18 files on main, 31 of them in
Python files touched by the preceding reformat commit. The lint step also
runs on whole changed files, so those files would have kept stalling
unrelated PRs even after the tree was formatted. Fix them so `ruff check`
and `ruff format --check` both pass for the whole tree.

23 findings are `ruff check --fix` autofixes (placeholder-less f-strings,
unused imports, multiple imports on one line). The rest:

- `import mooncake.pg` in legacy_buffer_perf.py is kept with
  `# noqa: F401`: the import registers the process-group backend, so
  removing it would change behavior.
- The availability probes in test_spdk_tgt_create.py and
  test_engram_store.py (import after sys.path setup) keep their imports
  with `# noqa`.
- Removed the shadowed duplicate `TestClass.serialize_into`; the
  surviving no-argument definition is the one Python already bound.
- Moved module-level imports in rl_samples.py and test_put_get_tensor.py
  into the import block, put the test_drain_http_api.py docstring above
  `from __future__ import annotations`, narrowed a bare `except` to
  `(TypeError, ValueError)`, and renamed an ambiguous `l` loop variable.
@alogfans

Copy link
Copy Markdown
Collaborator

@stmatengss

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants