Python conventions¶
TL;DR
- Ports are
Protocols, never ABCs; everything is a frozen, slotted, keyword-only dataclass unless a marked comment says why not. - Constants live in
constants.py, typedFinaland documented. Everything else is private unless re-exported. - Every function, field and MCP tool is documented in Google style, and a checker enforces it.
- The rules are enforced by tests, ruff, ty and fallow (What enforces it).
Rules¶
Protocol, not ABC. Ports are structural. Adapters and use cases never inherit from them;tychecks conformance where they are wired (CatalogModule.build(repository=...)). Importingabc.ABCis banned by ruff. The only inheritance in the codebase is exceptions.-
Immutable by default.
@dataclass(frozen=True, slots=True, kw_only=True)for entities, value objects, use cases, modules and adapters;ConfigDict(frozen=True, extra="forbid")for DTOs. A dataclass that cannot follow it says why on its decorator line, with a non-empty reason (several markers are comma-separated):Marker Drops Example # conventions: allow-mutable-dataclass -- <reason>frozenallow-positionalkw_onlya one-field value object built like a conversion,
Starlette middlewareallow-dictslots- Constants in constants.py. A module-level constant (anUPPER_CASEname, or anythingFinal) lives only in its package'sconstants.py, or in aconstants/package whose__init__.pyre-exports the public ones from private topic modules (constants/_git.py, seeingestion).- Each is typed Final[<type>]and followed by an attribute docstring:```python SESSION_IDLE: Final[timedelta] = timedelta(days=7) """Idle time after which a web session expires.""" ```constantsis a domain's innermost layer: it imports only the kernel and the standard library, so a value that needs a domain type is keyed by the enum's values ("queued", notJobState.QUEUED) or becomes a method.- Module-level framework objects (SQLAlchemy tables and clauses, a
TypeAdapter, a parser, a lock) are not constants: they stay with the adapter that uses them and are named in lower case. - Lower case is reserved for such objects. A fixed value (a number, a string, a pattern, a set
or mapping of literals) is a constant whatever its name, and the checker only sees it when it
is
UPPER_CASEorFinal, so write it that way. - Enum members and
__all__are not constants. - A chained assignment (
A = B = 1) and an assignment in a module-levelif/try/withblock define constants too. - Private by default. A module, function, class or constant that is not part of its package's
public API is private (
_name); what other packages use is re-exported by the package's__init__.pyand listed in__all__. Within a domain, a private name may be imported from the module that owns it (a private constant fromconstants). - Docstrings everywhere, Google style. Every function, method and nested function has one,
private ones included (ruff's own rules skip them). It lists every parameter under
Args:(self/clsexcluded), the result underReturns:(Yields:for a generator; a property describes the attribute instead) and raised domain errors underRaises:. - MCP tool descriptions come from a constant. FastMCP publishes a tool function's docstring
as the tool's description,
Args:andReturns:included. So a new tool passesdescription=from aFinalstring in its domain'sconstants.py(written for the agent that picks the tool) and keeps a full Google docstring on the function, asarticle_outlinedoes. The conventions test fails on any undocumented tool function. - Field docstrings. Every dataclass field and every pydantic field has an attribute docstring
on the line after it. Pydantic models set
use_attribute_docstrings=True(the domains'_Dtobases do), so the docstring is the field's description in OpenAPI and the MCP tool schemas;Field(description=...)is only for descriptions shared through a constant. - Use cases are callables.
await catalog.list_libraries()reads like the port name, and anyasync defwith the same signature is a valid test double. - Explicit composition root, no DI container.
bootstrap/container.pybuilds everything once, in plain code. Tests build modules directly with in-memory adapters. - Python 3.14 idioms. Lazy annotations (PEP 649, no
from __future__and noTYPE_CHECKINGblocks),typealiases (PEP 695),typing.Self,NewTypefor identifiers. - Errors. Domains raise kernel error subclasses (
NotFoundError...). The HTTP adapter maps them to status codes in one place (bootstrap/http.py); MCP returns them as tool errors. - Async all the way. Ports are
async; synchronous libraries are wrapped in adapters.
What enforces it¶
| Rule | Enforced by |
|---|---|
| Domain isolation, layer direction, adapter rules, framework-free core, no cycles, kernel purity |
ArchUnitPython,packages/exactdocs/tests/architecture/test_architecture.py |
| Fixed module set per domain, driving/driven split | same file (filesystem check) |
| Bridges point one way: retrieval to catalog, ingestion to catalog / retrieval |
test_bridge_direction.py |
No domain imports cryptography (the secret box is bootstrap's) |
test_architecture.py |
No abc.ABC, no relative imports, every ruff rule incl. preview,Google docstrings that document every returned value and raised exception |
ruff1 |
Constants placement, Final and docstrings; docstrings on every functionwith complete Args: / Returns:; field docstrings; use_attribute_docstrings;dataclass flags and exception markers |
test_conventions.py2 |
| Protocol conformance, types | ty |
Web: features don't import each other; shared imports no feature;routes use feature public APIs |
fallow (web/.fallowrc.json) + Biome noRestrictedImports |