DEV: Claude Code ruff hooks (auto-format on edit + pre-push lint guard)#1046
Merged
Conversation
…guard) Recurring red on the Linters CI job (all ruff-format failures) motivated two project-level Claude Code hooks, wired in .claude/settings.json: - PostToolUse (Edit|Write|MultiEdit): ruff_on_edit.py runs `ruff format` + `ruff check --fix` on the edited .py file (~instant), keeping edits clean. - PreToolUse (Bash): ruff_guard_push.py blocks `git push` when `ruff check` or `ruff format --check` would fail, before it turns CI red. Both invoke ruff as `<python> -m ruff`, are cross-platform (pure Python, no shell/OS-specific paths), and no-op when ruff is not installed. Pylint stays in CI only (too slow per-file for an interactive guard). See .claude/hooks/README.md. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
808518b to
a68c19a
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #1046 +/- ##
===========================================
+ Coverage 80.27% 81.52% +1.25%
===========================================
Files 104 118 +14
Lines 12769 15172 +2403
===========================================
+ Hits 10250 12369 +2119
- Misses 2519 2803 +284 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Motivation
The
LintersCI job has gone red repeatedly on trivial, auto-fixable ruff format issues (e.g. a long line indocs/conf.pyon #1045). These are caught in ~0.15s locally but only surface after a full CI round-trip.This adds two project-level Claude Code hooks so anyone using Claude Code in this repo keeps their edits lint-clean and can't push a ruff failure into CI.
What it does
.claude/hooks/ruff_on_edit.pyPostToolUseonEdit/Write/MultiEditruff format+ruff check --fixon the edited.pyfile. Surfaces unfixable issues back to Claude..claude/hooks/ruff_guard_push.pyPreToolUseonBashgit push, runsruff check+ruff format --check; blocks the push if either would fail.Wired in
.claude/settings.json(existingpermissionsblock preserved).Design
<python> -m ruff.ruffisn't installed; the guard only acts on realgit pushcommands.See
.claude/hooks/README.mdfor details and manual-test instructions.Verification
.py, clean-pass / non-push no-op / dirty-block for the guard.PostToolUsehook fires live (auto-formatted a mis-formatted file on write) and thePreToolUseguard allowed this branch's clean push.🤖 Generated with Claude Code