Python Guidelines
Standards and best practices for Python development. Follow these guidelines when writing or modifying Python code.
Design Principles
Apply DRY, KISS, and SOLID consistently. Prefer functional methods where relevant; use classes for stateful behavior. Use composition with Protocol classes for interfaces rather than inheritance. Each module should have a single responsibility. Use dependency injection for class dependencies.
Code Style
- Naming: Descriptive yet concise names for variables, methods, and classes
- Documentation: Docstrings for all classes, functions, enums, enum values
- Type hints: Use consistently; avoid
Any unless necessary
- Imports: Avoid barrel exports in
__init__.py; prefer blank files
Type Annotations
- Use
dict, list instead of typing.Dict, typing.List
- Use
str | None instead of Optional[str]
- Include
from __future__ import annotations at top of files with type hints
- Prefer built-in types over typing module equivalents
Architecture
Dependency Injection
- Always inject dependencies via constructors or methods when using classes
- One service class per module (interface and class models allowed in addition)
- Use Protocol classes to define interfaces for dependency injection and testing
Module Organization
- Each module focuses on one concern with clear boundaries
- Extract reusable methods to avoid duplication
- Design for reusability across contexts
Environment Variables
- Use an
environment.py file with individual methods per variable (e.g., api_key() for API_KEY, database_url() for DATABASE_URL)
- Co-locate all environment access in one place per package for easier mocking in tests
Data Models
- Use Pydantic v2 for schemas, validation, and data models
- Leverage Pydantic's type validation, serialization, and configuration management
- Use Pydantic models for API request/response schemas, configuration objects, and data transfer objects
Testing
Structure
- Tests mirror
src/ directory structure
- Test methods start with
test_
- Use test class suites: for
def foo() create class TestFoo
- Keep names concise, omit class suite name from method
- Always check for appropriate unit tests when changing code
Quality
- Use AAA (Arrange, Act, Assert) pattern
- Tests should be useful, readable, concise, maintainable
- Avoid tests that create massive diffs or become burdensome
Tools
- Prefer
pytest over unittest
- Use
pytest-mock for mocking
- Use
conftest.py for shared fixtures
- Use
tests/__test_<package_name>__ for shared testing code
Implementation
When implementing Python code:
- Ensure code passes type checking and tests before committing
- Group related changes with tests in atomic commits
- Check for existing workflow patterns (spec-first, TDD, etc.) and follow them
References
- For adhoc Python scripts in uv-managed projects, see
references/uv-scripts.md.
- For monorepo-specific patterns using uv and Hatch, see
references/uv-monorepo.md.
1---2name: python3description: Python development guidelines and best practices. Use when working with Python code.4---5
6# Python Guidelines
7
8Standards and best practices for Python development. Follow these guidelines when writing or modifying Python code.
9
10## Design Principles
11
12Apply DRY, KISS, and SOLID consistently. Prefer functional methods where relevant; use classes for stateful behavior. Use composition with Protocol classes for interfaces rather than inheritance. Each module should have a single responsibility. Use dependency injection for class dependencies.
13
14## Code Style
15
16- **Naming**: Descriptive yet concise names for variables, methods, and classes
17- **Documentation**: Docstrings for all classes, functions, enums, enum values
18- **Type hints**: Use consistently; avoid `Any` unless necessary
19- **Imports**: Avoid barrel exports in `__init__.py`; prefer blank files
20
21## Type Annotations
22
23- Use `dict`, `list` instead of `typing.Dict`, `typing.List`
24- Use `str | None` instead of `Optional[str]`
25- Include `from __future__ import annotations` at top of files with type hints
26- Prefer built-in types over typing module equivalents
27
28## Architecture
29
30### Dependency Injection
31
32- Always inject dependencies via constructors or methods when using classes
33- One service class per module (interface and class models allowed in addition)
34- Use Protocol classes to define interfaces for dependency injection and testing
35
36### Module Organization
37
38- Each module focuses on one concern with clear boundaries
39- Extract reusable methods to avoid duplication
40- Design for reusability across contexts
41
42### Environment Variables
43
44- Use an `environment.py` file with individual methods per variable (e.g., `api_key()` for `API_KEY`, `database_url()` for `DATABASE_URL`)
45- Co-locate all environment access in one place per package for easier mocking in tests
46
47### Data Models
48
49- Use Pydantic v2 for schemas, validation, and data models
50- Leverage Pydantic's type validation, serialization, and configuration management
51- Use Pydantic models for API request/response schemas, configuration objects, and data transfer objects
52
53## Testing
54
55### Structure
56
57- Tests mirror `src/` directory structure
58- Test methods start with `test_`
59- Use test class suites: for `def foo()` create `class TestFoo`
60- Keep names concise, omit class suite name from method
61- Always check for appropriate unit tests when changing code
62
63### Quality
64
65- Use AAA (Arrange, Act, Assert) pattern
66- Tests should be useful, readable, concise, maintainable
67- Avoid tests that create massive diffs or become burdensome
68
69### Tools
70
71- Prefer `pytest` over `unittest`
72- Use `pytest-mock` for mocking
73- Use `conftest.py` for shared fixtures
74- Use `tests/__test_<package_name>__` for shared testing code
75
76## Implementation
77
78When implementing Python code:
79- Ensure code passes type checking and tests before committing
80- Group related changes with tests in atomic commits
81- Check for existing workflow patterns (spec-first, TDD, etc.) and follow them
82
83## References
84
85- For adhoc Python scripts in uv-managed projects, see `references/uv-scripts.md`.
86- For monorepo-specific patterns using uv and Hatch, see `references/uv-monorepo.md`.