Python Code Style
Style Tools
We use Ruff for both linting and formatting:- Linter: Catches code quality issues, unused imports, and potential bugs
- Formatter: Ensures consistent code formatting across the project
Ruff Configuration
Configuration is defined inbackend/ruff.toml:
Configuration Details
Line Length: 240 characters- Longer than PEP 8’s 79 characters
- Allows for more readable complex expressions
- Reduces need for line continuations
- Modern wide-screen displays accommodate longer lines
- Uses latest Python features
- Ruff suggests modern syntax improvements
E- pycodestyle error rulesF- Pyflakes rules (undefined names, unused imports)I- isort import sorting rulesUP- pyupgrade rules (syntax modernization)
")
- Consistent with JSON and YAML
- More readable for strings containing apostrophes
- Standard Python indentation
- More consistent across editors
Running Ruff
From thebackend/ directory:
Pre-commit Integration
Run Ruff automatically before committing:Python Style Guidelines
Type Hints
Always use type hints for function parameters and return values:typing module:
Imports
Order imports using isort rules (automatically handled by Ruff):- Standard library imports
- Third-party imports
- Local application imports
Naming Conventions
Modules and packages:lowercase_with_underscores
PascalCase
lowercase_with_underscores
UPPERCASE_WITH_UNDERSCORES
Docstrings
Use docstrings for modules, classes, and functions:Comments
Use comments sparingly - prefer self-documenting code:Function Length
Keep functions focused - aim for functions under 50 lines:Error Handling
Use specific exceptions:String Formatting
Use f-strings for string formatting:Dictionary and List Handling
Use dict.get() with defaults:Context Managers
Use context managers for resource management:Data Classes
Use Pydantic models for configuration and data validation:Frontend Code Style
TypeScript Guidelines
The frontend uses TypeScript with ESLint and Prettier:TypeScript Style Highlights
- Interfaces over types for object shapes
- Explicit return types for functions
- Functional components with hooks
- Named exports for components
- Async/await over promises
Documentation Standards
Code Documentation Policy
CRITICAL: Always update documentation after code changes When making code changes:- Update
README.mdfor user-facing changes - Update
CLAUDE.mdfor development/architecture changes - Update inline code documentation
- Update API documentation if endpoints change
- Add migration notes for breaking changes
Documentation Format
- Use clear, concise language
- Include code examples
- Explain why, not just what
- Keep documentation in sync with code
- Use proper Markdown formatting
Linting CI Integration
Linting runs automatically in CI:Configuration Files Reference
Backend Configuration
backend/ruff.toml - Ruff linter and formatter configuration
backend/pyproject.toml - Python project metadata and dependencies
Style Checklist
Before committing code:- Run
make formatto auto-fix style issues - Run
make lintto check for remaining issues - Run
make testto ensure tests pass - All functions have type hints
- Complex logic has explanatory comments
- Public functions have docstrings
- Imports are organized (automatic with Ruff)
- No unused imports or variables
- Documentation updated if needed