Python
Policy-level Python guidance. Load one focused ref for deep detail; keep this file as router + defaults.
Reference Map
| Need |
Ref |
| uv, deps, venv, Python |
references/uv.md |
| package/module layout |
references/structure.md |
| env/settings/secrets |
references/config.md |
| packaging/publishing |
references/packaging.md |
| style/ruff/naming |
references/style.md |
| typing/protocols |
references/types.md |
| validation/exceptions |
references/errors.md |
| design/SRP/composition |
references/design.md |
| anti-pattern review |
references/anti-patterns.md |
| asyncio/concurrency |
references/async.md |
| jobs/workers/queues |
references/jobs.md |
| resources/cleanup |
references/resources.md |
| retries/timeouts |
references/resilience.md |
| logs/metrics/tracing |
references/observability.md |
| profiling/perf |
references/perf.md |
| pytest/testing |
references/tests.md |
Assets
assets/project/pyproject.toml
assets/project/src/myapp/main.py
assets/project/src/myapp/settings.py
assets/project/tests/test_main.py
assets/project/scripts/report.py
Onboarding
Detect:
ls pyproject.toml 2>/dev/null
Before edit:
- verify tools
- find validation entrypoints
- inspect layout/config/tests
- check momentum:
git log --oneline -10
Validation discovery order:
- task aliases in
pyproject.toml
- direct
uv run ...
- README/scripts
- CI config
Default Stack
- Python 3.12+
uv
ruff
ty
pyright
pytest
rumdl
taskipy
pre-commit
bandit
rtk
Validation
uv run ruff format --check .
uv run ruff check .
uv run rumdl check .
uv run ty check
uv run pyright
uv run pytest -v
Prefer task aliases when repo defines them:
uv run task lint
uv run task fmt
uv run task type
uv run task test
uv run task test-cov
uv run task sec
Verify tools:
uv --version
python --version
uv run ruff --version
uv run ty --version
uv run pyright --version
uv run pytest --version
uv run rumdl --version
uv run task --version
uv run pre-commit --version
uv run bandit --version
Install deps:
uv sync
Core Defaults
- use
uv, uv run, uv add; avoid direct pip
- format with
ruff
- prefer
pathlib, f-strings, absolute imports
- type public APIs; use
Protocol at boundaries; contain Any
- externalize config/secrets with typed settings loaded at startup
- validate external input at boundaries; convert raw payloads early
- keep IO at edges; avoid leaking ORM/transport types
- prefer composition/focused modules over clever abstractions
- default sync unless real concurrent I/O pressure exists
- keep async path async end-to-end when used
- centralize retries, timeouts, cleanup
- make background jobs idempotent and explicit about state/ownership
- keep observability structured and outside core business logic
- test behavior and boundaries; avoid framework-heavy tests when simple tests suffice
- optimize only after measuring bottleneck
Build-Fix
Order:
- format
- lint
- markdown
- typing
- tests
After each fix, rerun failing check. Stop/report if fix needs architecture change.
Run uv run task sec for explicit security review; do not treat it as part of the default quick check unless the repo opts in.
Common fixes:
| Tool |
Signal |
Fix |
| ruff |
formatting/import/style |
format or remove/fix code |
| ty |
type/import mismatch |
fix type, import path, or dependency |
| pyright |
type/import mismatch |
fix type, import path, or dependency |
| pytest |
assertion/import/fixture |
fix logic, path, or fixture |
Debug
- reproduce exactly
- record env
- read traceback bottom-up
- inspect
git log --oneline -10 and git diff
- isolate boundary
- use
git bisect if regression likely
- confirm fix with failing test/command
- remove temp debug/breakpoints
Rules: do not guess first; fix root cause; one hypothesis at time; do not silence with broad try/except; do not change tests to match bug.
FastAPI Cues
- use
Annotated for Path, Query, Header, Depends
- reusable dependency aliases only when repeated signatures simplify
- no ellipsis
... for required FastAPI/Pydantic fields
- explicit return type or
response_model
- router-level
prefix, tags, dependencies
- default
def if internals may block
- no blocking code inside async handlers
- prefer HTTPX over Requests
- no deprecated
ORJSONResponse/UJSONResponse performance crutch
- no
RootModel when normal typed structure is enough
Review Focus
- correctness: edge cases, error paths, races
- security: boundary validation, secrets, injection, auth
- performance: blocking async, N+1, unbounded loops
- maintainability: SRP, dead code, magic values, noisy comments
- conventions: naming, style, project patterns
Skip style already enforced by tools.
Refactoring
- preserve behavior
- no hidden feature work
- do not rewrite stable code just because old
- reduce complexity only with clear maintenance gain
- one logical change at time
Workflow Cues
- API contract shape -> pair
design
- architecture/boundaries -> pair
arch
- test-first or failure diagnosis -> pair
quality
1---2name: python3description: Python guidance for project onboarding, uv, typing, testing, async, configuration, packaging, observability, resilience, resource management, FastAPI conventions, and common debug/build-fix patterns. Load when writing, reviewing, debugging, or maintaining Python code.4---56# Python78Policy-level Python guidance. Load one focused ref for deep detail; keep this file as router + defaults.910## Reference Map1112| Need | Ref |13| ----------------------- | ---------------------------------------- |14| uv, deps, venv, Python | `references/uv.md` |15| package/module layout | `references/structure.md` |16| env/settings/secrets | `references/config.md` |17| packaging/publishing | `references/packaging.md` |18| style/ruff/naming | `references/style.md` |19| typing/protocols | `references/types.md` |20| validation/exceptions | `references/errors.md` |21| design/SRP/composition | `references/design.md` |22| anti-pattern review | `references/anti-patterns.md` |23| asyncio/concurrency | `references/async.md` |24| jobs/workers/queues | `references/jobs.md` |25| resources/cleanup | `references/resources.md` |26| retries/timeouts | `references/resilience.md` |27| logs/metrics/tracing | `references/observability.md` |28| profiling/perf | `references/perf.md` |29| pytest/testing | `references/tests.md` |3031## Assets3233- `assets/project/pyproject.toml`34- `assets/project/src/myapp/main.py`35- `assets/project/src/myapp/settings.py`36- `assets/project/tests/test_main.py`37- `assets/project/scripts/report.py`3839## Onboarding4041Detect:4243```bash44ls pyproject.toml 2>/dev/null45```4647Before edit:48491. verify tools502. find validation entrypoints513. inspect layout/config/tests524. check momentum: `git log --oneline -10`5354Validation discovery order:55561. task aliases in `pyproject.toml`572. direct `uv run ...`583. README/scripts594. CI config6061## Default Stack6263- Python 3.12+64- `uv`65- `ruff`66- `ty`67- `pyright`68- `pytest`69- `rumdl`70- `taskipy`71- `pre-commit`72- `bandit`73- `rtk`7475## Validation7677```bash78uv run ruff format --check .79uv run ruff check .80uv run rumdl check .81uv run ty check82uv run pyright83uv run pytest -v84```8586Prefer task aliases when repo defines them:8788```bash89uv run task lint90uv run task fmt91uv run task type92uv run task test93uv run task test-cov94uv run task sec95```9697Verify tools:9899```bash100uv --version101python --version102uv run ruff --version103uv run ty --version104uv run pyright --version105uv run pytest --version106uv run rumdl --version107uv run task --version108uv run pre-commit --version109uv run bandit --version110```111112Install deps:113114```bash115uv sync116```117118## Core Defaults119120- use `uv`, `uv run`, `uv add`; avoid direct `pip`121- format with `ruff`122- prefer `pathlib`, f-strings, absolute imports123- type public APIs; use `Protocol` at boundaries; contain `Any`124- externalize config/secrets with typed settings loaded at startup125- validate external input at boundaries; convert raw payloads early126- keep IO at edges; avoid leaking ORM/transport types127- prefer composition/focused modules over clever abstractions128- default sync unless real concurrent I/O pressure exists129- keep async path async end-to-end when used130- centralize retries, timeouts, cleanup131- make background jobs idempotent and explicit about state/ownership132- keep observability structured and outside core business logic133- test behavior and boundaries; avoid framework-heavy tests when simple tests suffice134- optimize only after measuring bottleneck135136## Build-Fix137138Order:1391401. format1412. lint1423. markdown1434. typing1445. tests145146After each fix, rerun failing check. Stop/report if fix needs architecture change.147Run `uv run task sec` for explicit security review; do not treat it as part of the default quick check unless the repo opts in.148149Common fixes:150151| Tool | Signal | Fix |152| ------- | ------------------------ | ------------------------------------ |153| ruff | formatting/import/style | format or remove/fix code |154| ty | type/import mismatch | fix type, import path, or dependency |155| pyright | type/import mismatch | fix type, import path, or dependency |156| pytest | assertion/import/fixture | fix logic, path, or fixture |157158## Debug1591601. reproduce exactly1612. record env1623. read traceback bottom-up1634. inspect `git log --oneline -10` and `git diff`1645. isolate boundary1656. use `git bisect` if regression likely1667. confirm fix with failing test/command1678. remove temp debug/breakpoints168169Rules: do not guess first; fix root cause; one hypothesis at time; do not silence with broad `try/except`; do not change tests to match bug.170171## FastAPI Cues172173- use `Annotated` for `Path`, `Query`, `Header`, `Depends`174- reusable dependency aliases only when repeated signatures simplify175- no ellipsis `...` for required FastAPI/Pydantic fields176- explicit return type or `response_model`177- router-level `prefix`, `tags`, dependencies178- default `def` if internals may block179- no blocking code inside async handlers180- prefer HTTPX over Requests181- no deprecated `ORJSONResponse`/`UJSONResponse` performance crutch182- no `RootModel` when normal typed structure is enough183184## Review Focus185186- correctness: edge cases, error paths, races187- security: boundary validation, secrets, injection, auth188- performance: blocking async, N+1, unbounded loops189- maintainability: SRP, dead code, magic values, noisy comments190- conventions: naming, style, project patterns191192Skip style already enforced by tools.193194## Refactoring195196- preserve behavior197- no hidden feature work198- do not rewrite stable code just because old199- reduce complexity only with clear maintenance gain200- one logical change at time201202## Workflow Cues203204- API contract shape -> pair `design`205- architecture/boundaries -> pair `arch`206- test-first or failure diagnosis -> pair `quality`