Overview
Pre-commit hooks run automated checks on your code before it’s committed to version control. This ensures code quality and consistency across the team by catching issues early in the development process.Pre-commit hooks run locally on your machine, providing immediate feedback before code even reaches CI/CD pipelines.
What’s Included
This project includes pre-commit hooks for Python code quality:- Ruff: Fast Python linter and code checker
- Black: Uncompromising Python code formatter
Installation
Follow these steps to set up pre-commit hooks in your development environment:Install pre-commit
Install the pre-commit package using pip:Or add it to your
pyproject.toml dev dependencies:Install git hooks
Install the git hook scripts:This creates a
.git/hooks/pre-commit script that runs automatically.Hook Details
Ruff
Ruff is an extremely fast Python linter written in Rust. It replaces multiple tools (Flake8, isort, pyupgrade, and more) with a single, blazingly fast linter. What it checks:- Code style violations (PEP 8)
- Unused imports and variables
- Common programming errors
- Security issues
- Import sorting
--fix to automatically fix issues where possible:
pyproject.toml:
Black
Black is “the uncompromising Python code formatter.” It reformats your code to ensure consistent style across your entire codebase. What it does:- Formats Python code to a consistent style
- Handles line length (default 88 characters)
- Manages whitespace and indentation
- Formats strings, imports, and expressions
pyproject.toml:
Black is intentionally opinionated with minimal configuration options. This design choice eliminates debates about code style.
How Pre-commit Works
Normal Commit Flow
When you rungit commit, pre-commit automatically:
When Hooks Fail
If pre-commit modifies files or finds unfixable issues:-
Review the changes made by the hooks:
-
Stage the fixed files:
-
Commit again:
Running Hooks Manually
Check All Files
Run hooks on all files in the repository:- First setting up pre-commit
- After changing hook configuration
- Before making a pull request
Check Specific Files
Run hooks on specific files:Run Individual Hooks
Run only one hook:Skip Hooks
Temporarily skip hooks (use sparingly):Updating Hooks
Pre-commit hooks pin specific versions (e.g.,rev: v0.5.6). Update them periodically:
.pre-commit-config.yaml with the latest versions:
Adding More Hooks
Extend.pre-commit-config.yaml with additional hooks:
Type Checking with Mypy
Security Scanning with Bandit
Markdown Linting
General File Checks
Team Setup
For New Team Members
Add setup instructions to your project README:Enforcing Pre-commit
While pre-commit hooks run locally, you can’t force team members to install them. Best practices:CI Validation
Run the same checks in CI (already configured in workflows.yml)
Troubleshooting
Hook not running
Hook not running
If pre-commit doesn’t run on commit:
-
Verify installation:
-
Check git hooks are installed:
-
Reinstall if missing:
Ruff and Black conflict
Ruff and Black conflict
Ruff and Black can sometimes conflict on line length or formatting. To resolve:
-
Ensure both use the same line length:
-
Configure Ruff to ignore checks handled by Black:
Hooks are slow
Hooks are slow
If hooks take too long:
- Pre-commit only runs on changed files (not —all-files)
- Check for network issues (hooks download on first run)
- Consider running expensive checks (like mypy) only in CI
- Use pre-commit’s caching (enabled by default)
Can't commit urgent fix
Can't commit urgent fix
In emergencies, you can skip hooks:Then fix the code quality issues in a follow-up commit:
Configuration Examples
Minimal Configuration
For a simple Python project:Comprehensive Configuration
For production projects:Best Practices
Install Immediately
Run
pre-commit install as part of your initial project setupKeep Updated
Run
pre-commit autoupdate monthly to get latest improvementsAuto-fix When Possible
Use
--fix args to reduce manual interventionMirror CI Checks
Run the same tools in CI and pre-commit for consistency
Related Resources
- GitHub Actions Workflows - CI/CD automation
- Templates - Issue and PR templates
- Pre-commit Documentation
- Ruff Documentation
- Black Documentation