How to Run Code Quality Tools¶
This guide explains how to use the linters, formatters, and type checkers configured for this project.
Overview¶
The project uses three main code quality tools:
- ruff — Fast Python linter
- black — Python code formatter
- mypy — Static type checker
Installing Tools¶
All code quality tools are included in requirements.txt:
Running Ruff (Linter)¶
Check for Issues¶
Auto-fix Issues¶
Check Specific Files¶
Common Ruff Checks¶
Ruff checks for: - Unused imports - Undefined variables - PEP 8 style violations - Common bugs and anti-patterns - Import sorting
Running Black (Formatter)¶
Check Formatting¶
Check if files need formatting without modifying them:
Format Code¶
Auto-format all Python files:
Format Specific Files¶
Black Configuration¶
Black is configured with default settings (88 character line length).
Running Mypy (Type Checker)¶
Check All Files¶
Check Specific Module¶
Common Type Issues¶
Mypy checks for: - Type annotation correctness - Function signature compatibility - Attribute existence - Return type consistency
Mypy Configuration¶
Configuration is in mypy.ini:
[mypy]
python_version = 3.11
warn_return_any = True
warn_unused_configs = True
disallow_untyped_defs = False # Enable gradually
Running All Tools Together¶
Sequential Execution¶
With Auto-fixing¶
Pre-commit Checks¶
Before committing code, run all quality checks:
# Auto-fix what can be fixed
ruff check . --fix
black .
# Verify everything passes
ruff check .
black --check .
mypy .
CI/CD Integration¶
Code quality tools run automatically in CI (.github/workflows/ci.yml):
- name: Ruff (lint)
run: ruff check .
- name: Black (format check)
run: black --check .
- name: Mypy (type checks)
run: mypy .
Tool-Specific Configuration¶
Ruff Configuration¶
Ruff uses default configuration. To customize, create pyproject.toml:
Black Configuration¶
Black uses default settings. To customize, add to pyproject.toml:
Mypy Configuration¶
Mypy configuration is in mypy.ini. Customize as needed:
[mypy]
python_version = 3.11
warn_return_any = True
warn_unused_configs = True
check_untyped_defs = True
Common Workflows¶
Before Committing¶
# Fix formatting and auto-fixable issues
black .
ruff check . --fix
# Verify all checks pass
ruff check .
black --check .
mypy .
# Run tests
pytest -q
Fixing Specific Issues¶
Unused Imports¶
Ruff will identify and can auto-remove:
Formatting Issues¶
Black will auto-format:
Type Errors¶
Mypy errors usually require manual fixes:
- Run
mypy .to see errors - Add type annotations or fix type mismatches
- Run
mypy .again to verify fixes
IDE Integration¶
VS Code¶
Install extensions: - Ruff — Official Ruff extension - Black Formatter — Microsoft's Black extension - Mypy Type Checker — Mypy extension
Configure in .vscode/settings.json:
{
"[python]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "ms-python.black-formatter"
},
"python.linting.enabled": true,
"python.linting.ruffEnabled": true,
"mypy-type-checker.args": ["--config-file=mypy.ini"]
}
PyCharm¶
- Ruff: Install via Settings → Plugins → Marketplace → "Ruff"
- Black: Configure as External Tool or use the Black plugin
- Mypy: Configure as External Tool or use Mypy plugin
Troubleshooting¶
Ruff and Black Conflict¶
If Ruff and Black disagree on formatting:
- Black has priority for formatting
- Run black . first, then ruff check . --fix
Mypy False Positives¶
If mypy reports incorrect errors:
- Add # type: ignore comment for specific lines
- Use more specific type annotations
- Update mypy.ini configuration
Performance Issues¶
If tools are slow:
- Limit scope: ruff check core/ instead of ruff check .
- Use caching: Ruff and Black cache results by default
- Exclude virtual environments: Add .venv/ to .gitignore
Best Practices¶
- Run locally before pushing — Catch issues early
- Use auto-fix capabilities — Let tools handle simple fixes
- Add type hints gradually — Don't try to type everything at once
- Configure IDE integration — Get real-time feedback
- Keep tools updated —
pip install --upgrade ruff black mypy
Next Steps¶
- Run Tests — Execute the test suite
- Set Up Local Development — Configure your development environment
- CI/CD Configuration — Understanding automated checks
Related Documentation¶
- Contributing Guidelines — Code contribution standards
- Testing Strategy — Testing approach