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

Ruff format after write

After every Python file write or edit, runs ruff format via uv to apply consistent style without any agent intervention.

Use cases

  • PEP 8 compliance without thinking about it
  • Consistent formatting across a team
  • Reduced review noise on style changes

Providers & tags

Claude Code
#validation#ruff#format#python#quality

settings.json fragment

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

Script · .claude/hooks/ruff-format.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 format "${filePath}"`, { timeout: 15_000, stdio: 'pipe' });
} catch {
  // uv/ruff not installed — skip silently
}