This guide covers the setup and usage of pre-commit hooks in the Gadugi project.
# Install pre-commit hooks
uv run pre-commit install
# Run hooks on all files (verify setup)
uv run pre-commit run --all-files# Install pre-commit hooks
pre-commit install
# Run hooks on all files (verify setup)
pre-commit run --all-filesOur pre-commit configuration automatically runs these checks:
- ruff: Python linting with auto-fixes
- ruff-format: Code formatting
- debug-statements: Removes debug print statements
- pyright: Type checking (runs on push)
- trailing-whitespace: Removes trailing spaces
- end-of-file-fixer: Ensures files end with newline
- check-yaml: Validates YAML syntax
- check-merge-conflict: Detects merge conflict markers
- detect-secrets: Finds potential secrets in code
- pytest: Runs test suite before pushing
- pyright: Type checking validation
Once installed, hooks run automatically:
git commit -m "Your commit message"
# Hooks run automatically before commitRun hooks manually anytime:
# Run all hooks on changed files
uv run pre-commit run # UV projects
pre-commit run # Standard projects
# Run all hooks on all files
uv run pre-commit run --all-files # UV projects
pre-commit run --all-files # Standard projects
# Run specific hook
uv run pre-commit run ruff # UV projects
pre-commit run ruff # Standard projects
# Run type checking specifically (pre-push stage)
uv run pre-commit run pyright --hook-stage pre-push # UV projects
pre-commit run pyright --hook-stage pre-push # Standard projectsHook installation failed:
# Reinstall hooks
uv run pre-commit uninstall
uv run pre-commit installPre-commit command not found:
# For UV projects - pre-commit should be in pyproject.toml
uv sync --all-extras
# For standard projects - install pre-commit
pip install pre-commitHooks failing on commit:
# See detailed output
uv run pre-commit run --all-files --verbose
# Skip hooks temporarily (not recommended)
git commit -m "message" --no-verifySpecific hook failing:
# Run individual hook to see details
uv run pre-commit run ruff --verbose
uv run pre-commit run trailing-whitespace --verbose
# Debug pyright type checking issues
uv run pre-commit run pyright --hook-stage pre-push --verbose
pyright container_runtime/ # Run pyright on specific directoryUpdate hook versions:
# Update to latest versions
uv run pre-commit autoupdate
# Commit the updated .pre-commit-config.yaml
git add .pre-commit-config.yaml
git commit -m "Update pre-commit hook versions"Our .pre-commit-config.yaml configuration:
repos:
# Python code quality
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.4
hooks:
- id: ruff
args: [ --fix ] # Auto-fix issues where possible
- id: ruff-format
# General file quality
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
args: ['--maxkb=1000']
- id: check-case-conflict
- id: check-merge-conflict
- id: check-toml
- id: debug-statements
- id: mixed-line-ending
args: ['--fix=lf']
# Security scanning
- repo: https://github.com/Yelp/detect-secrets
rev: v1.5.0
hooks:
- id: detect-secrets
args: ['--baseline', '.secrets.baseline']
exclude: .*\.lock$|package-lock\.json$
# Type checking (runs on push)
- repo: local
hooks:
- id: pyright
name: pyright type checker
entry: pyright
language: system
types: [python]
pass_filenames: false
stages: [pre-push]
# Testing (runs on push, not commit)
- repo: local
hooks:
- id: pytest
name: pytest
entry: uv run pytest # Use 'pytest' for non-UV projects
language: system
pass_filenames: false
always_run: true
stages: [pre-push]
# Global settings
default_language_version:
python: python3.13
default_stages: [pre-commit, pre-push]
# Files to exclude from all hooks
exclude: |
(?x)^(
.*\.skip$|
\.venv/.*|
venv/.*|
\.git/.*|
\.pytest_cache/.*|
__pycache__/.*|
\.worktrees/.*
)- Install hooks immediately after cloning repository
- Run manually before important commits
- Don't skip hooks unless absolutely necessary
- Fix issues rather than bypassing checks
- Update regularly to get latest improvements
- Standardize on UV for consistent tooling
- Document exceptions when hooks must be skipped
- Review hook failures in code reviews
- Update configurations as project needs evolve
- Monitor hook performance to prevent slow commits
Pre-commit hooks integrate with our development workflow:
- Phase 6 Testing: Agents verify all hooks pass
- PR Creation: Hooks must pass before PR creation
- Code Review: Hook failures prevent PR approval
- CI/CD: Hooks run again in continuous integration
This ensures consistent code quality across all development activities.
Pyright provides static type checking for Python code, helping catch type-related errors before runtime. It's configured to run during the pre-push stage to avoid slowing down commits.
Pyright is configured via pyrightconfig.json:
{
"typeCheckingMode": "standard",
"pythonVersion": "3.11",
"pythonPlatform": "All",
"reportMissingImports": "warning",
"reportMissingTypeStubs": "none",
"include": ["**/*.py"],
"exclude": [".venv", ".git", ".worktrees", "__pycache__"]
}- Docker Import Handling: Uses
TYPE_CHECKINGguards for optional dependencies - Standard Mode: Balanced type checking that catches errors without being too strict
- Import Warnings: Reports missing imports but allows development flexibility
- CI Integration: Runs automatically on push to catch type issues early
Common Docker Import Errors:
# ✅ Correct approach using TYPE_CHECKING
from typing import TYPE_CHECKING
if TYPE_CHECKING:
import docker
else:
docker = None
try:
import docker # type: ignore[import-untyped]
docker_available = True
except ImportError:
docker_available = FalseType Ignore Comments:
# Use specific ignore codes for better maintainability
docker.from_env() # type: ignore[attr-defined]Running Pyright Manually:
# Check specific files
pyright container_runtime/
# Get verbose output
pyright --verbose
# Generate statistics
pyright . --stats- Phase 6 Testing: WorkflowManager agents verify pyright passes
- Pre-push Hooks: Automatic type checking before code sharing
- PR Reviews: Type issues block PR approval
- CI/CD: Additional verification in continuous integration
This multi-layer approach ensures type safety without impeding development velocity.