Pre-Commit Validation
This skill ensures code quality and test coverage before committing changes to the repository.
Purpose
Run comprehensive validation locally before committing to:
- •Catch issues early (before CI runs)
- •Ensure all tests pass
- •Verify code quality (linting, formatting, type checking)
- •Maintain code coverage standards
- •Reduce CI failures and iteration time
Validation Checklist
Before creating any commit, ensure ALL of the following pass:
1. Run Tests
# Run all tests pytest tests/ # Or with coverage (recommended) pytest tests/ --cov=src --cov-report=term
Requirements:
- •✅ All tests must pass (exit code 0)
- •✅ No test failures or errors
- •✅ Coverage should not decrease (if checking coverage)
2. Run Code Quality Checks
# Check formatting black --check src/ tests/ # Check linting ruff check src/ tests/ # Check types mypy src/ --strict --explicit-package-bases
Requirements:
- •✅ All checks must pass (exit code 0)
- •✅ No formatting issues
- •✅ No linting errors
- •✅ No type errors
3. Run Pre-commit Hooks (Optional but Recommended)
# Run all pre-commit hooks manually pre-commit run --all-files
Note: Pre-commit hooks run automatically on git commit, but running them manually gives faster feedback.
Workflow Integration
When to Use This Skill
Use this skill in the following scenarios:
- •Before any commit: Always run validation before creating a commit
- •After implementing features: Before committing new code
- •After refactoring: Before committing refactored code
- •Before pushing: As a final check before pushing to remote
- •When pre-commit is skipped: If using
git commit --no-verify, run validation manually
Integration with Phase Implementation
When implementing features following the phase-implementation skill:
- •Implement code changes
- •Run pre-commit validation (this skill)
- •Fix any issues found
- •Repeat validation until all checks pass
- •Commit changes (following commit-format skill)
- •Push to GitHub
Quick Validation Command
For convenience, you can run all checks in sequence:
# Run all validation checks pytest tests/ --cov=src --cov-report=term && \ black --check src/ tests/ && \ ruff check src/ tests/ && \ mypy src/ --strict --explicit-package-bases
If all commands succeed (exit code 0), you're ready to commit.
Auto-fix Common Issues
Many issues can be auto-fixed:
# Auto-fix formatting black src/ tests/ # Auto-fix linting issues (many can be fixed automatically) ruff check --fix src/ tests/
After auto-fixing, re-run validation to ensure everything passes.
Handling Failures
Test Failures
- •Review test output: Understand why tests are failing
- •Fix the code: Address the root cause
- •Re-run tests: Verify the fix works
- •Run full test suite: Ensure no regressions
Code Quality Failures
- •Formatting issues: Run
black src/ tests/to auto-fix - •Linting errors: Run
ruff check --fix src/ tests/to auto-fix many issues - •Type errors: Fix type annotations or add type ignores (with justification)
- •Re-run checks: Verify all issues are resolved
Coverage Decreases
- •Review coverage report: Identify uncovered code
- •Add tests: Write tests for uncovered code paths
- •Verify coverage: Ensure coverage meets or exceeds threshold (80%)
Best Practices
- •Run validation frequently: Don't wait until the end - validate after each logical change
- •Fix issues immediately: Address problems as soon as they're found
- •Don't skip validation: Even for small commits, run at least basic checks
- •Use pre-commit hooks: They provide automatic validation, but manual checks are still valuable
- •Check coverage regularly: Ensure test coverage doesn't regress
Relationship to CI/CD
Important: Local validation does NOT replace CI/CD checks.
- •Local validation (this skill): Fast feedback, catch issues early
- •CI/CD pipeline: Final gate, runs on all pushes and PRs
Even if local validation passes, CI may still catch:
- •Environment-specific issues
- •Issues missed locally
- •Integration problems
However, running validation locally:
- •Reduces CI failures
- •Provides faster feedback
- •Saves CI resources
- •Improves development workflow
Exceptions
When to Skip Validation
Validation should only be skipped in exceptional circumstances:
- •WIP commits: Work-in-progress commits with
[WIP]prefix (but still validate before final commit) - •Merge commits: Usually safe to skip (CI will validate)
- •Documentation-only changes: May skip type checking, but still run formatting/linting
Note: Even in exceptions, consider running at least formatting checks to maintain code quality.
See Also
- •Development Workflow - General development guidelines
- •Pre-commit Hooks Setup - Automatic validation setup
- •Commit Message Conventions - Commit format guidelines