Ruff Linter and Formatter
The project uses Ruff, a fast Python linter and formatter written in Rust. Ruff combines the functionality of multiple tools (Flake8, Black, isort, etc.) into a single, blazing-fast tool.Running Ruff
Ruff Configuration
Ruff is configured inpyproject.toml:
pyproject.toml
What Ruff Checks
Code Style
Code Style
- Line length (default 88 characters)
- Indentation and whitespace
- Quote style consistency
- Trailing commas
Import Organization
Import Organization
- Import sorting (isort-compatible)
- Unused imports
- Import order and grouping
- Duplicate imports
Code Quality
Code Quality
- Unused variables
- Undefined names
- Syntax errors
- Common anti-patterns
- Type annotation issues
Best Practices
Best Practices
- Docstring presence and format
- Naming conventions
- Complexity warnings
- Security vulnerabilities
Pre-commit Hooks
Pre-commit hooks automatically check code quality before each commit, ensuring that only clean code enters the repository.Installing Pre-commit Hooks
Running Pre-commit Manually
Configured Hooks
The project uses the following pre-commit hooks (configured in.pre-commit-config.yaml):
Standard Pre-commit Hooks
check-ast
Validates Python syntax by checking the abstract syntax tree
trailing-whitespace
Removes trailing whitespace from all files
check-docstring-first
Ensures docstrings appear before code in modules
check-json
Validates JSON file syntax (excludes .vscode)
check-yaml
Validates YAML file syntax
check-merge-conflict
Detects merge conflict markers in files
check-executables-have-shebangs
Ensures executable files have proper shebangs
debug-statements
Detects debug statements like pdb.set_trace()
requirements-txt-fixer
Sorts and fixes requirements.txt files
mixed-line-ending
Ensures consistent line endings (LF)
Ruff Hooks
ruff
Runs Ruff linter with auto-fix on Python, .pyi, and Jupyter filesArguments:
--fix, --exit-non-zero-on-fix, --show-fixesruff-format
Runs Ruff formatter on Python, .pyi, and Jupyter files
Notebook Hooks
nbstripout
Strips output from Jupyter notebooks before committingThis keeps notebooks clean and prevents large diffs from cell outputs.
Pre-commit Configuration
The complete.pre-commit-config.yaml configuration:
.pre-commit-config.yaml
Bypassing Hooks
In rare cases, you may need to bypass pre-commit hooks:Code Quality Workflow
Follow this workflow to maintain code quality:CI Code Quality Checks
Code quality checks run automatically in CI on every push and pull request:- Ruff linting: Checks for code quality issues
- Ruff formatting: Verifies code is properly formatted
- Pre-commit hooks: All configured hooks run in CI
- Type checking: Static type analysis (if configured)
Best Practices
General Guidelines
General Guidelines
- Follow PEP 8 style guide
- Write clear, descriptive variable and function names
- Keep functions small and focused
- Add docstrings to all public functions and classes
- Use type hints for function parameters and return values
Import Organization
Import Organization
Organize imports in three groups:
- Standard library imports
- Third-party imports
- Local application imports
Docstrings
Docstrings
Use Google-style or NumPy-style docstrings:
Error Handling
Error Handling
- Use specific exception types
- Provide informative error messages
- Clean up resources in finally blocks
- Use context managers (with statements) when possible
Next Steps
- Learn about the Release Process
- Understand Versioning strategy
- Review Testing guidelines