Skip to content

DEV: Claude Code ruff hooks (auto-format on edit + pre-push lint guard)#1046

Merged
Gui-FernandesBR merged 1 commit into
developfrom
dev/claude-ruff-hooks
Jul 4, 2026
Merged

DEV: Claude Code ruff hooks (auto-format on edit + pre-push lint guard)#1046
Gui-FernandesBR merged 1 commit into
developfrom
dev/claude-ruff-hooks

Conversation

@Gui-FernandesBR

Copy link
Copy Markdown
Member

Motivation

The Linters CI job has gone red repeatedly on trivial, auto-fixable ruff format issues (e.g. a long line in docs/conf.py on #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

Hook Event Behavior
.claude/hooks/ruff_on_edit.py PostToolUse on Edit/Write/MultiEdit Runs ruff format + ruff check --fix on the edited .py file. Surfaces unfixable issues back to Claude.
.claude/hooks/ruff_guard_push.py PreToolUse on Bash Before a git push, runs ruff check + ruff format --check; blocks the push if either would fail.

Wired in .claude/settings.json (existing permissions block preserved).

Design

  • Fast only: both hooks run just ruff (~0.15s whole-repo). Pylint is intentionally left to CI — its per-file startup (~3s) is too slow for an interactive guard, and ruff is what has actually been breaking the job.
  • Cross-platform (Windows/macOS/Linux): pure Python, no shell built-ins or OS-specific paths; ruff invoked as <python> -m ruff.
  • Never in the way: silent no-op if ruff isn't installed; the guard only acts on real git push commands.

See .claude/hooks/README.md for details and manual-test instructions.

Verification

  • Both scripts unit-tested via piped hook payloads: format-on-edit, no-op on non-.py, clean-pass / non-push no-op / dirty-block for the guard.
  • Confirmed the PostToolUse hook fires live (auto-formatted a mis-formatted file on write) and the PreToolUse guard allowed this branch's clean push.

🤖 Generated with Claude Code

@Gui-FernandesBR Gui-FernandesBR requested a review from a team as a code owner July 4, 2026 17:23
…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>
@Gui-FernandesBR Gui-FernandesBR force-pushed the dev/claude-ruff-hooks branch from 808518b to a68c19a Compare July 4, 2026 17:26
@Gui-FernandesBR Gui-FernandesBR merged commit e06b267 into develop Jul 4, 2026
7 checks passed
@Gui-FernandesBR Gui-FernandesBR deleted the dev/claude-ruff-hooks branch July 4, 2026 17:31
@codecov

codecov Bot commented Jul 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.52%. Comparing base (9cf3dd4) to head (a68c19a).
⚠️ Report is 105 commits behind head on develop.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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