This guide explains how to use the quality check tools to ensure code quality before committing changes.
The RAG system includes comprehensive quality checks to maintain high code standards:
- Code Formatting & Linting: Automatic code formatting and linting with Ruff
- Type Checking: Static type analysis with Pyright
- Security: Security vulnerability scanning with Bandit
- Testing: Unit tests with pytest and coverage reporting
- Git Status: Check for uncommitted changes
# On Windows
pre-commit-check.bat
# On Linux/Mac or using Python directly
python pre-commit-check.pypython pre-commit-check.py --fixpython pre-commit-check.py --skip-testspython pre-commit-check.py --verboseIf you have make installed, you can use convenient shortcuts:
# Run all quality checks
make check
# Run checks and auto-fix issues
make check-fix
# Run checks without tests (faster)
make check-fast
# Run only tests
make test
# Run tests with coverage
make coverage
# Format code
make format
# Check formatting without changes
make format-check
# Run linting
make lint
# Run type checking
make type-check
# Run security checks
make security
# Clean build artifacts
make cleanRuff is an extremely fast Python linter and formatter that replaces multiple tools (Black, isort, flake8, pylint).
# Check formatting
python -m ruff format --check rag_system tests
# Auto-format code
python -m ruff format rag_system tests
# Check linting
python -m ruff check rag_system tests
# Auto-fix linting issues
python -m ruff check --fix rag_system testsConfiguration: See pyproject.toml under [tool.ruff]
Features:
- Combines formatting (like Black)
- Import sorting (like isort)
- Linting (like flake8, pylint)
- 10-100x faster than traditional tools
- Auto-fix capabilities
Pyright is a fast, feature-rich type checker for Python.
python -m pyright rag_systemConfiguration: See pyproject.toml under [tool.pyright]
Features:
- Fast type checking
- Better error messages than mypy
- Supports latest Python features
- IDE integration (VS Code)
Bandit scans for common security issues.
python -m bandit -r rag_system -llConfiguration: See pyproject.toml under [tool.bandit]
Severity Levels:
-ll: Only report medium and high severity issues
pytest runs all unit tests with coverage reporting.
# Run all tests
python -m pytest tests/ -v
# Run with coverage
python -m pytest tests/ -v --cov=rag_system --cov-report=term-missing --cov-report=html
# Run specific test file
python -m pytest tests/test_config.py -v
# Run specific test
python -m pytest tests/test_config.py::TestRAGConfig::test_default_config -vConfiguration: See pyproject.toml under [tool.pytest.ini_options]
Coverage Requirements: Minimum 80% code coverage
# Install all development dependencies
pip install -r requirements-dev.txt
# Or install individually
pip install ruff pyright bandit pytest pytest-cov pytest-asyncioruff>=0.1.0
pyright>=1.1.0
bandit>=1.7.5
pytest>=7.3.0
pytest-cov>=4.1.0
pytest-asyncio>=0.21.0
To automatically run checks before each commit, create .git/hooks/pre-commit:
#!/bin/bash
# Run quality checks before commit
echo "Running pre-commit quality checks..."
python pre-commit-check.py --skip-tests
if [ $? -ne 0 ]; then
echo ""
echo "Quality checks failed. Commit aborted."
echo "Fix the issues or use 'git commit --no-verify' to skip checks."
exit 1
fi
exit 0Make it executable:
chmod +x .git/hooks/pre-commitIf you need to commit without running checks:
git commit --no-verify -m "Your commit message"Create .github/workflows/quality-checks.yml:
name: Quality Checks
on: [push, pull_request]
jobs:
quality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Install dependencies
run: |
pip install -r requirements.txt
pip install -r requirements-dev.txt
- name: Run quality checks
run: python pre-commit-check.pyIf you get "Tool not found" errors, install the missing tool:
pip install ruff pyright bandit pytest pytest-covIf Pyright reports type errors:
- Check the error message carefully
- Add type hints where missing
- Use
# type: ignorefor false positives (with explanation) - Configure
typeCheckingModeinpyproject.tomlif needed
If coverage is below 80%:
- Add tests for uncovered code
- Check coverage report:
htmlcov/index.html - Identify missing test cases
If Ruff can't auto-fix an issue:
- Read the error message
- Fix manually
- Run
ruff checkagain to verify
-
Run checks before committing: Always run
make checkorpython pre-commit-check.py -
Fix issues incrementally: Don't accumulate technical debt
-
Use auto-fix when possible: Run with
--fixflag to automatically fix formatting and linting -
Write tests first: Maintain high test coverage (>80%)
-
Review security warnings: Don't ignore bandit warnings
-
Keep dependencies updated: Regularly update development tools
-
Document exceptions: If you need to ignore a warning, add a comment explaining why
- Speed: 10-100x faster than traditional tools
- All-in-one: Replaces Black, isort, flake8, pylint
- Auto-fix: Automatically fixes many issues
- Modern: Written in Rust, actively maintained
- Compatible: Drop-in replacement for existing tools
- Fast: Much faster than mypy
- Accurate: Better type inference
- Modern: Supports latest Python features
- IDE Integration: Built into VS Code
- Clear Errors: Better error messages
- pyproject.toml: Configuration for Ruff, Pyright, pytest, bandit
- pre-commit-check.py: Main quality check script
- Makefile: Convenient command shortcuts
The quality check system ensures:
✓ Consistent code formatting
✓ PEP 8 compliance
✓ High code quality
✓ Type safety
✓ Security best practices
✓ Comprehensive test coverage
✓ Clean git history
Run python pre-commit-check.py before every commit to maintain these standards!
If you're migrating from the old setup:
- Black + isort → Ruff format (formatting)
- flake8 + pylint → Ruff check (linting)
- mypy → Pyright (type checking)
Ruff and Pyright are faster, more modern, and easier to configure!