Files
gh-basher83-lunar-claude-pl…/skills/python-code-quality/patterns/pre-commit-integration.md
2025-11-29 18:00:18 +08:00

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 clean then pre-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

  1. Keep hooks fast - Pre-commit should be < 10 seconds
  2. Auto-fix when possible - Use --fix for ruff
  3. Document skip policy - When is --no-verify acceptable?
  4. Update regularly - Run pre-commit autoupdate monthly