1.8 KiB
1.8 KiB
Pre-commit Integration for Ruff and Pyright
Run quality checks automatically before each commit to prevent bad code from entering the repository.
Setup
1. Install pre-commit
pip install pre-commit
2. Create .pre-commit-config.yaml
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.0
hooks:
- id: ruff
args: [--fix]
- id: ruff-format
- repo: https://github.com/RobertCraigie/pyright-python
rev: v1.1.380
hooks:
- id: pyright
3. Install hooks
pre-commit install
Usage
Pre-commit hooks now run automatically:
git add .
git commit -m "feat: add feature"
# Hooks run automatically before commit
Skip hooks (when needed)
git commit --no-verify -m "wip: work in progress"
Manual Runs
Run hooks on all files:
pre-commit run --all-files
Run specific hook:
pre-commit run ruff --all-files
pre-commit run pyright --all-files
Configuration
Ruff with auto-fix
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Pyright with specific directories
- id: pyright
files: ^(src|tests)/
Troubleshooting
Hook fails with "command not found":
- Ensure ruff/pyright installed in environment
- Try:
pre-commit cleanthenpre-commit install
Hooks too slow:
- Run only on changed files (default behavior)
- Skip pyright in pre-commit, run in CI instead
Want to update hook versions:
pre-commit autoupdate
Best Practices
- Keep hooks fast - Pre-commit should be < 10 seconds
- Auto-fix when possible - Use
--fixfor ruff - Document skip policy - When is
--no-verifyacceptable? - Update regularly - Run
pre-commit autoupdatemonthly