Stack: Python {{MIN PYTHON — e.g. 3.11}}+ on Linux and macOS, packaged with
pyproject.toml, tested with pytest. The minimum version is the baseline for both the
language and the standard library; pick the oldest CPython still receiving security
fixes that every deployment target actually has, and check before assuming. Features
newer than the baseline are listed under Version-gated features below together with
the baseline way to do the same thing. Interpreters/platforms:
{{INTERPRETERS & PLATFORMS — e.g. CPython 3.11–3.13 on Linux x86_64/aarch64 and macOS arm64}}.
Environment/dependency tool: {{TOOL — e.g. uv, or pip + venv with pip-tools}}.
pyproject.tomlis the single source of project metadata and tool configuration (ruff, mypy, pytest, coverage). Nosetup.py,setup.cfg,requirements*.txtedited by hand, or per-tool config files whenpyproject.tomlcan hold the same settings.- The minimum version is set in one place,
requires-python = ">={{MIN PYTHON}}", and every tool reads its target from there or is set to match it: rufftarget-version, mypypython_version. That way a linter or type checker flags syntax and stdlib APIs newer than the baseline. srclayout: the package lives insrc/{{PACKAGE}}/, tests intests/. Tests run against the installed package (pip install -e ./uv sync), never viasys.pathmanipulation or by running from the repo root.- Every environment is a virtual environment. Never
pip installinto the system or Homebrew Python, and neversudo pip. - Direct dependencies in
pyproject.tomlhave lower bounds (and upper bounds only for a known break). Exact, hashed versions for every transitive dependency live in a committed lock file ({{LOCK FILE — e.g. uv.lock, or requirements.lock from pip-compile}}) that CI installs from. - A runnable program is a
[project.scripts]entry point or a__main__.py(python -m {{PACKAGE}}), not a loose script that imports fromsrc/by path. - No generated or environment files in git:
.venv/,__pycache__/,*.egg-info/,dist/,build/,.pytest_cache/,.mypy_cache/.
- Type hints on every public function, method, and class attribute, and on anything
non-trivial internally.
{{TYPE CHECKER — e.g. mypy --strict, or pyright strict}}runs clean.Anyand# type: ignore[code](always with the specific code) only with a comment saying why. @dataclass(frozen=True, slots=True)(ortyping.NamedTuple) for plain value carriers instead of dicts or tuples passed around by position.Enumover string or int constants.- Never use a mutable default argument (
def f(x=[])); useNoneand create it inside. - Errors:
- Raise specific exceptions, ideally a small project hierarchy rooted at one base class, so callers can catch the package's errors in one place.
- No bare
except:, and noexcept Exception:except at a top-level boundary (CLImain, a request handler, a worker loop), and there, log it with the traceback (log.exception(...)). - When translating an exception, chain it:
raise ConfigError(...) from e. - Never swallow an exception (
except ...: pass) without a comment saying why it is safe. - Don't use
assertto validate input or enforce invariants that matter in production —python -Ostrips it. Raise instead.
- Every file, socket, lock, subprocess, or temp directory the code opens is managed by
a
withblock (orcontextlib.ExitStack). Don't rely on garbage collection to close things. pathlib.Pathfor filesystem paths, not string concatenation oros.pathin new code.datetimeobjects are timezone-aware (datetime.now(timezone.utc)), never naive;datetime.utcnow()is deprecated. Pass a clock (a callable returning the time) into code that reads the time so tests can fix it.decimal.Decimal(constructed from astr, not afloat) for money or any value that must be exact.- Imports: absolute imports within the package, one module per line, no
from x import *, no imports with side effects at module level (no work at import time beyond defining things). Module-level mutable state only with a comment saying who mutates it. - Logging through
logging.getLogger(__name__). Library code never callsprint, and never configures logging (nobasicConfig) — only the entry point does. Use lazy%-style arguments in log calls (log.debug("x=%s", x)), not f-strings. - Concurrency: pick one model per component —
asyncio, threads (concurrent.futures.ThreadPoolExecutor), or processes (ProcessPoolExecutor) — and don't call blocking I/O from inside a coroutine (useasyncio.to_thread). Shared mutable state between threads is guarded by a lock that the code names in a comment. Threads don't speed up CPU-bound pure-Python code (the GIL); use processes for that. - Off-limits:
eval/execon anything not written by this repo;pickle/shelveon untrusted data;subprocesswithshell=True(pass an argument list);yaml.loadwithoutSafeLoader; monkey-patching third-party or stdlib modules outside tests. - Syntax and stdlib APIs newer than the baseline are off-limits (ruff and the type
checker, targeted at the baseline, flag most of them). Don't route around the
baseline with
sys.version_infobranches ortry: import ...fallbacks except in one compat module with a test for each branch. If a newer feature is truly needed, raising the baseline is a decision to record as an ADR.
Raising the baseline is one change: bump requires-python, ruff target-version, mypy
python_version, the CI interpreter matrix, and the lock file, then record an ADR.
Nothing in a row is allowed until the baseline is at or above that row's version. After
the baseline moves, prefer the newer form in new code; existing code is converted only
when touched for another reason, or as a TASK-### (ruff check --select UP --fix
does much of it mechanically).
| Need | Older way | Newer way (min. version) |
|---|---|---|
| Union / optional types | Optional[X], Union[X, Y] |
X | None, X | Y (3.10) |
| Branch on the shape of data | if isinstance(...) / elif chains |
match / case with class and mapping patterns (3.10) — not as a substitute for a plain if/elif on values |
| Zip iterables that must be equal length | zip, then check lengths |
zip(a, b, strict=True) (3.10) |
| Read TOML | Third-party tomli |
tomllib (3.11) |
| Method returning its own class | TypeVar bound to the class |
typing.Self (3.11) |
| Run and await a group of tasks | asyncio.gather |
asyncio.TaskGroup (3.11), which cancels siblings on failure |
| Report several errors at once | A list attribute on a custom exception | ExceptionGroup and except* (3.11) |
| Add context to an exception in flight | Wrap and re-raise | e.add_note(...) (3.11) |
| Generic classes/functions and aliases | TypeVar / Generic[T] / TypeAlias |
class Box[T]:, def f[T](x: T), type Alias = ... (3.12) |
| Mark an override | Comment | @typing.override (3.12) |
| Chunk an iterable | Hand-written loop or zip(*[iter(x)] * n) |
itertools.batched(x, n) (3.12) |
| Mark API deprecated | warnings.warn(..., DeprecationWarning) |
@warnings.deprecated (3.13) |
Still off-limits at any baseline: free-threaded (no-GIL) or JIT builds as an assumption, and any interpreter other than CPython (e.g. PyPy), unless the project lists it above.
- Always name the encoding:
open(path, encoding="utf-8"),Path.read_text(encoding="utf-8"),subprocess.run(..., text=True, encoding="utf-8"). Before Python 3.15 the default comes from the locale, so it differs between machines (and between a shell and a cron job or container). Ruff'sPLW1514catches the omission. - Don't depend on the default locale for anything machine-readable (number formatting,
str.loweris fine butlocale.format_stringis not); read times as UTC. - The interpreter is
python3, neverpython(which may be missing or Python 2 on some systems). Scripts start with#!/usr/bin/env python3, and the tooling runs inside the venv (uv run ..., or.venv/bin/python -m ...). multiprocessingdefault start method differs:spawnon macOS,forkon Linux before 3.14 (forkserverfrom 3.14). Code that uses processes must work underspawn: everything sent to a worker is picklable, worker functions are importable module-level functions, and the entry point is guarded byif __name__ == "__main__":. Set the start method explicitly if behavior depends on it.- macOS filesystems are usually case-insensitive and Linux's are case-sensitive: module names, resource names, and file names must match case exactly. An import that works on a Mac can fail on Linux.
- Package data is read with
importlib.resources.files(...), never by building a path from__file__. - Subprocesses get an argument list, not a shell string, and don't assume GNU tools
(
sed -i,readlink -f,date -d) are on thePATH. Temp space comes fromtempfile(respects$TMPDIR), never a hardcoded/tmp/.... - Write
\nexplicitly in machine-readable output, and open text files for writing withnewline="\n"when the exact bytes matter.
{{TEST COMMAND — e.g. uv run pytest, or python -m pytest}}is the single command that runs every test. A test pytest doesn't collect doesn't exist; configuretestpathsand--strict-markersinpyproject.toml.- Unit tests live in
tests/, use plainassertwith pytest's introspection (notunittest.TestCase-styleassertEqualin new code), fixtures over setup/teardown methods, and@pytest.mark.parametrizeover loops in a test. Mocking (unittest.mock,monkeypatch) is for collaborators the test can't run for real — not for the code under test. - Baseline tests — where the output is a text artifact (serialized response,
generated metadata, CLI output) — compare actual output to a checked-in
*.baselinefile undertests/, fail on any difference, and print a unified diff (difflib.unified_diff). Baselines are read and written as UTF-8 with\nline endings. They change only through one deliberate update mode that rewrites them instead of comparing:{{UPDATE MECHANISM — e.g. a --update-baselines pytest option defined in conftest.py}}. Never hand-edit a baseline. Update mode is off by default and never on in CI. Reviewgit diffon baseline files like source code; a baseline change in a PR needs a reason. - Tests that need files use pytest's
tmp_path, never a fixed path or the working directory. Tests that start servers bind to port0and read back the port. - Tests are deterministic and independent: no reliance on test order, wall-clock time
(inject a fixed clock), network, the default locale/encoding/time zone, environment
variables the test didn't set (
monkeypatch.setenv), or module-level state left over from another test — sopytest -n auto(pytest-xdist) and random ordering are always safe. Fixed seeds only. - New logic isn't done without a test. A bug fix isn't done without a regression test that fails before the fix and passes after.
- Coverage is measured with
coverage.py/pytest-covin CI, branch coverage on:{{COVERAGE GATE — e.g. report only, or fail_under = 85}}. A gate is a floor, not a goal; don't write tests that only execute lines. - CI runs the suite on every interpreter in the matrix, including the baseline version — passing on the newest Python says nothing about the oldest.
- Warnings are errors in tests (
filterwarnings = ["error"]), with specific third-party warnings ignored by name and a comment saying why. ADeprecationWarningtoday is a break on the next Python release.
ruff formatis the formatting source of truth — don't hand-format around it.ruff check(rule selection committed inpyproject.toml, including at leastE,F,I,B,UP,PLW1514) and{{TYPE CHECKER}}run clean before a change is considered finished, or a new finding is suppressed with# noqa: CODE/# type: ignore[code]and a comment saying why. Never a bare# noqa.- Optional but recommended: run both through
pre-commitwith the hook versions pinned in.pre-commit-config.yaml, and the same checks again in CI.
- Every public module, class, function, and method gets a docstring in
{{DOCSTRING STYLE — e.g. Google or NumPy style}}: what it does, each argument and the return value (don't repeat the type hints, do say whatNonemeans), the exceptions a caller should expect, units where relevant, and thread/async-safety when it isn't obvious. Ruff'sDrules, set to that convention, enforce presence and shape.