Python Standards¶
Consistency matters more than personal preference. The goal is a codebase that anyone on the team can read and modify safely.
Principles¶
- Prefer explicit over implicit
- Favour readability over brevity
- Use type hints — they document intent and catch errors early
- Keep functions small and focused on one thing
Default Toolchain¶
Unless the team already has a working setup, start here:
- uv for environments, dependencies and running everything (
uv run,uv add,uvxfor one-off tools) pyproject.tomlas the single source: Python version, dependencies, and dependency groups for anything not needed at runtime, so deploy images stay lean. Commit the lock file- ruff for linting and formatting, run before every commit
- mypy in strict mode on shared code
- pytest for tests, see Unit Tests
- Configuration from one place per concern (an environment variable or a config file), never hardcoded per call site
Suggested Tooling¶
Alternatives, if the team has a reason to differ. Choose what fits and enforce it consistently.
| Tool | Purpose |
|---|---|
ruff |
Fast linting and formatting (can replace black + isort + flake8) |
black |
Opinionated code formatter |
isort |
Import sorting |
mypy |
Static type checking |
flake8 |
Style and error checking |
Pick one formatting approach and stick to it. Mixing tools that do overlapping things creates friction.
Style Guidance¶
- Use descriptive names —
get_active_customers()overget_data() - Avoid magic numbers — assign them to named constants
- Document the why when behaviour is non-obvious, not the what
- Avoid deeply nested logic — extract to functions
Type Hints¶
def calculate_revenue(orders: list[Order], currency: str = "GBP") -> Decimal:
...
Type hints are especially valuable at function boundaries and in shared libraries.