Python Conventions
Apply these conventions when working on Python files or projects.
Dispatch
| $ARGUMENTS | Action |
|---|---|
| Active (auto-invoked when working on Python files) | Apply all conventions below |
| Empty | Display convention summary |
check | Verify tooling compliance only |
References
| File | Purpose |
|---|---|
references/exceptions.md | When to break conventions (legacy, corporate) |
Tooling
- Package manager:
uvfor all Python operations (uv run python ...instead ofpython,uv addinstead ofpip install) - Project config:
pyproject.tomlwithuv add <pkg>(neveruv pip installorpip install) - Type checking:
ty(not mypy) - Linting:
ruff checkandruff format - Testing:
uv run pytest - Task running:
uv run <command>for all script execution
Virtual Environments
- Let
uvmanage virtual environments automatically - Never manually create or activate
.venvdirectories - Use
uv runto execute within the project environment
Preferred Libraries
When applicable, prefer these libraries over alternatives:
| Purpose | Library | Instead of |
|---|---|---|
| Logging | loguru | logging |
| Retries | tenacity | manual retry loops |
| Progress bars | tqdm | print statements |
| Web APIs | fastapi | flask |
| CLI tools | typer | argparse, click |
| DB migrations | alembic | manual SQL |
| DB ORM | sqlmodel | sqlalchemy raw |
| UI/demos | gradio | streamlit |
| Numerics | numpy | manual math |
| MCP servers | fastmcp | raw MCP protocol |
Project Structure
- Use
pyproject.tomlfor all project metadata and dependencies - Place source code in a package directory matching the project name
- Use
uvworkspace members for monorepo sub-packages - Run
ruff checkandruff formatbefore committing
Critical Rules
- Always use
uvfor package management -- neverpip installorpip - Use
uv addto add dependencies -- neveruv pip install - Use
tyfor type checking -- nevermypy - Run
uv run pytestbefore committing any Python changes - Run
uv run ruff checkto lint before committing - Check
references/exceptions.mdbefore breaking any convention - Prefer libraries from the preferred table over their alternatives
Canonical terms (use these exactly):
uv-- the required package manager and task runnerty-- the required type checker (not mypy)ruff-- the required linter and formatterpyproject.toml-- the single source of project configurationuv run-- prefix for all Python command execution