HookStackGitHub
Back to catalogue
ValidationPostToolUse· Write|EditPostToolUseAfter tool execution · non-blocking· non-blocking

Ruff lint after write

Runs ruff check --fix on the modified Python file so lint violations are reported to the agent immediately and auto-fixable rules are applied inline.

Use cases

  • Catch unused imports and undefined names before they accumulate
  • Auto-fix trivial violations (isort, unnecessary pass, etc.)
  • Enforce project ruff rules without a CI round-trip

Providers & tags

Claude Code
#validation#ruff#lint#python#quality

settings.json fragment

{
  "hooks": {
    "PostToolUse": [
      {
        "hooks": [
          {
            "command": "node $CLAUDE_PROJECT_DIR/.claude/hooks/ruff-check.mjs",
            "type": "command"
          }
        ],
        "matcher": "Write|Edit"
      }
    ]
  }
}

Script · .claude/hooks/ruff-check.mjs

#!/usr/bin/env node
import { readFileSync } from 'fs';
import { execSync } from 'child_process';

const input = JSON.parse(readFileSync(0, 'utf8'));
const filePath = input.tool_input?.file_path ?? input.tool_input?.path ?? '';

if (!filePath.endsWith('.py')) process.exit(0);

try {
  execSync(`uv run ruff check --fix "${filePath}"`, {
    encoding: 'utf8',
    timeout: 15_000,
    stdio: 'pipe',
  });
} catch (e) {
  if (e.stdout) process.stderr.write(`[ruff-check] ${e.stdout}\n`);
}