Format Python Files Check
Comprehensive hook designed for automatically, format, python, files. Includes structured workflows, validation checks, and reusable patterns for post tool.
Format Python Files Check
Automatically formats Python files with Black after every edit operation, enforcing PEP 8 compliant code style across the project.
When to Use This Hook
Attach this hook when you need to:
- Apply consistent Black formatting to every Python file Claude modifies during a session
- Eliminate style inconsistencies in Python codebases without manual formatter invocation
- Ensure all edited
.pyfiles comply with your team's Black configuration
Consider alternatives when:
- Your project uses a different formatter like
yapforautopep8with custom rules - You prefer formatting only at pre-commit time using the
pre-commitframework
Quick Start
Configuration
name: format-python-files-check type: hook trigger: PostToolUse category: post-tool
Example Trigger
# Hook triggers after Claude edits a Python file claude> Edit src/services/user_service.py # Black runs automatically on the edited file
Example Output
reformatted src/services/user_service.py
All done!
1 file reformatted.
Core Concepts
Format Scope Overview
| Aspect | Details |
|---|---|
| File Extensions | .py only |
| Matcher | Triggers on Edit tool operations |
| Formatter | black (must be installed via pip) |
| Error Handling | Suppressed with 2>/dev/null || true |
| Config Resolution | Uses nearest pyproject.toml [tool.black] section |
Formatting Workflow
Edit Tool Completes
|
Check Extension
/ \
.py Other
| |
Run Black Skip
|
āāā“āāā
| |
OK Fail
| |
Done Suppress
Error
Configuration
| Parameter | Type | Default | Description |
|---|---|---|---|
matcher | string | "Edit" | Tool operation that triggers formatting |
line_length | number | 88 | Maximum line length (Black default) |
target_version | string | "py310" | Python version target for syntax |
suppress_errors | boolean | true | Silently ignore formatter failures |
check_only | boolean | false | Report formatting issues without fixing them |
Best Practices
-
Install Black in Your Virtual Environment - The hook calls
blackdirectly, so it must be available on PATH. Install it in your project's virtualenv withpip install blackso it picks up the correct version. -
Configure in pyproject.toml - Define Black settings in
pyproject.tomlunder[tool.black]so every developer and this hook use identical formatting rules. Avoid relying on command-line flags. -
Add a check_only Mode for CI - In CI pipelines, run Black with
--checkto fail on unformatted files rather than silently fixing them. The hook itself should auto-fix during development sessions. -
Extend to Write Operations - Add a second matcher for the
Writetool so newly created Python files are also formatted immediately, not just edited ones. -
Pair with isort - Black handles code formatting but not import ordering. Add a companion hook that runs
isortbefore Black to keep imports organized and avoid conflicts between the two tools.
Common Issues
-
Black Not Installed - If Black is missing, the hook silently suppresses the error. Developers may not realize files are unformatted. Add a startup check or use
pip install blackin your project setup script. -
Line Length Conflicts with Flake8 - Black defaults to 88-character lines while Flake8 defaults to 79. Align both tools by setting
max-line-length = 88in your Flake8 config. -
Magic Trailing Comma Behavior - Black preserves trailing commas and forces multi-line formatting when they are present. If this surprises your team, document the behavior and decide on a trailing comma convention.
Reviews
No reviews yet. Be the first to review this template!
Similar Templates
Pre-Commit Security Scanner
Pre-commit hook that scans staged files for hardcoded secrets, API keys, passwords, and sensitive data patterns before allowing commits.
Agents Md Watcher
Streamline your workflow with this automatically, loads, agents, configuration. Includes structured workflows, validation checks, and reusable patterns for automation.
Automated Build Inspector
Boost productivity using this automatically, trigger, build, processes. Includes structured workflows, validation checks, and reusable patterns for automation.