Python Development
pyproject.toml is the source of truth for the Python target, tools, scripts, and dependencies. Check it, .python-version, and CI before using 3.12-only syntax. Project conventions win over these defaults.
Toolchain
- uv for environments and commands (
uv run,uv add); never callpipdirectly. Match the lockfile already in use. - Ruff for lint and format. Pyright for types, or
tyonly when the project has adopted it. - pytest for tests.
Defaults
- Stdlib before packages:
argparse,dataclasses,pathlib,json,urllib,logging. Do not add Rich, pydantic, or dotenv only for polish. - 3.12+ typing:
X | Y, builtin generics, PEP 695 generics andtypealiases. Keep legacyTypeVarstyle when editing pre-3.12 modules. @dataclass(frozen=True, slots=True)for small values;TypedDictwithNotRequiredat recurring JSON boundaries;Protocolowned by the consumer. Keepdict[str, Any]at the boundary only.- Import collection ABCs from
collections.abc. TakeSequence[T]for read-only inputs. - Wrap errors with
raise DomainError(...) from exc. Catch broad exceptions only where they become an exit code, response, log entry, or re-raise. - Async:
asyncio.TaskGroupfor sibling tasks,asyncio.timeoutaround external waits. Keep references to background tasks so their exceptions surface. - Text I/O takes an explicit
encoding. Sort glob results when order reaches output or tests. - Libraries log through
logging; they never print diagnostics.
CLIs
argparsefor small tools; Click or Typer only when the project already uses them.main(argv: Sequence[str] | None = None) -> int; callasyncio.runonly there. Expose it via[project.scripts]and keeppython -m pkgworking.- Config precedence: flag, env, config file, default.
References
- testing.md: read when adding or reshaping tests, or when the suite is slow.
- linting.md: read when changing Ruff or type-checker config, or the lint/type-check commands.
Done when the relevant build/test/lint checks pass on what you changed, or you name each check that did not run and why.