Documentation

Docs / Reference

ADR 0015 — Python module resolution and FastAPI routes

Status: Accepted (developer review, 2026-10-10) Date: 2026-10-10

Context

The T011c investigation found 23 literal generated SDK operations and 23 backend registrations in the pinned full-stack-fastapi-template. Existing imports reached its package files, but callable-only binding resolution could not follow router/settings assignments or initializer re-exports. Route composition needs the same current declarations as import and call resolution.

Options considered

Decision

Use the shared resolver in internal/lang/python described in python-resolution.md for imports, calls, framework constructors, route endpoints and prefixes. Metadata is read from the current scan; internal per-file caches retain scopes, imports, bindings, class bodies and registration syntax only. Constructor bindings retain only syntax used by resolution, rather than arbitrary dependency/model argument payloads. Per-pass module and completed callable memoization do not persist, and per-name cycle guards never memoize incomplete re-export traversals. Aggregate extraction version becomes extractors-9.

FastAPI recognition requires an external import of exactly FastAPI/APIRouter from fastapi, FastAPI from fastapi.applications, or APIRouter from fastapi.routing. Scanned shadow modules win. Per-file Python syntax stays in the language package; composition stays in internal/extract/routes, using the same path entry shape as Express. All FastAPI apps are roots. Router constructor prefixes, nested includes and mounts concatenate before normalization. Named handlers get fact, confidence-1 handler-to-route exposes_route edges with registration evidence. Decorators hash only their call expression, excluding @ and handler bodies. Source identities follow ADR 0013, including declaration-order suffixes.

Prefixes accept literals, stable module literal assignments and source-backed settings defaults. settings_default is environment-overridable: the own class literal comes from a direct imported BaseSettings subclass and an unoverridden module instance. Arbitrary keyword unpacking is unresolved because it may override the attribute. Class-body conditionals cannot prove an unconditional default. Resolution strength is settings_default < constant < literal; a chain uses the weakest and duplicate templates retain the strongest proof. Conditional chains merge to unconditional if any identical path is unconditional.

The task's intended deviations are explicit:

  1. Includes are order-insensitive live compositions, although FastAPI copies registrations at include time.
  2. Normalization removes trailing slashes except root.
  3. Starlette convertor suffixes are stripped; {p:path} becomes {p} without modeling multiple segments.
  4. Resolution uses scanned project roots rather than interpreter sys.path.
  5. Every FastAPI app is a composition root, including mounted sub-apps.
  6. Settings defaults ignore environment variables, dotenv files, validators and Field defaults.

Real-semantics corrections: methods=None and empty method lists/tuples/sets default to GET. Starlette treats colons/asterisks outside brace parameters as literal path characters, so FastAPI does not apply Express substitutions. Python's package finder prefers regular packages to source modules, and source modules to namespace directories; intermediate source modules cannot contain submodules. Unknown keyword unpacks do not prove omitted prefix/method defaults. Bare function names skip an enclosing class namespace. Explicit assignment aliases do not establish constant prefixes.

Contract v1, store interface, schema, dependencies and non-route source identity policy remain unchanged. This extends ADRs 0006 and 0013 without editing them.

Consequences

Settings and target-module changes relink cached importers and recompose routes. Handler body edits preserve route hashes. The aggregate version invalidates all language caches once. Source evidence and conservative diagnostics remain available without evaluating Python or importing project code.

Known gaps

No type inference, inheritance traversal, star-import expansion, arbitrary assignment flow or evaluation of constructor/factory return values. Function locals count only in their binding function. FastAPI dependencies are not middleware links; websocket registrations are excluded from HTTP routes. Unicode named string escapes require a Unicode name table and stay unresolved. Postgres parity and execution on other operating systems belong to CI.

Review notes (developer, 2026-10-10)

Revision 1 (T035: mounted FastAPI apps)

Status: Accepted (delegated review, 2026-11-24)

Context

Intended deviation 5 ("every FastAPI app is a composition root, including mounted sub-apps") stored, for a mounted app, full paths the served app answers 404 for: with app.mount("/sub", sub), sub's @sub.get("/ping") was stored as /ping beside /sub/ping. FastAPI 0.121.1 (Starlette 0.49.3) and 0.165.0 (Starlette 1.10.0) serve a mounted app only below its mount. The T035 task record (internal) has the diagnosis and the oracle.

Options considered

Decision

Deviation 5 is superseded: every FastAPI app is a root unless a Starlette mount consumes it. x.mount(p, app) consumes app whatever p and x are; a constant p on a proven x composes the paths through the mount, any other p is a hop (unresolved_mounts), an unread x gives no path (route_mount_unresolved). x.router.mount(p, app) on a proven app x is x.mount(p, app). A Starlette Mount(p, app) (starlette.routing.Mount, also through fastapi.routing) consumes app; written directly in the routes= list literal of a proven FastAPI(...) constructor (outside a helper context) it composes as mount on that app, anywhere else it gives no path. A mount in a test-scope file consumes only a test-scope app. The syntax keeps Mount(...) calls and whether each is an element of a routes= list literal (a cached syntax change; extractors version).

Step EM2 adds three parts (link-time; no further syntax change):

Consequences

A mounted app's routes have only the paths the served app answers; an unmounted app keeps its own root (the app-root policy for apps served on their own). Routes of apps mounted through a form not composed lose their path (precision-safe). Golden outputs of mounted sub-apps lose the own-root path; OpenAPI documents lose the matching operation.

Known gaps

An app that is both mounted and served on its own through a form outside the closed list (uvicorn or gunicorn on the command line, a Dockerfile or Procfile, uvicorn.run("pkg.mod:app") as a string, a Config/Server pair) loses its own-root paths. Mount in an APIRouter(routes=...) list, a routes list bound to a name or built elsewhere, and app.router reassigned to another router are not read; such a list is also not read for shadowing. Route order between two routes (not a mount) of one app is not modelled, nor shadowing below an unresolved-mount hop (unresolved_mounts paths). FastAPI releases after 0.165.0 are assumed to keep 0.137.0's included-router mounts.

Revision 1 review notes (2026-11-24): confirmed pre-existing wrong facts (mounted Express and FastAPI sub-apps stored at their own root; the real frameworks answer 404 there) fixed with oracles on Express 4.21.1/5.1.0 and FastAPI 0.121.1/0.136.3/0.137.0/0.165.0; one independent review and a confirmation pass (no open finding); gate run 45311511149 green on Linux, Postgres and Windows; no corpus route changed except conditional marks with shown assumptions (RealWorld 21, seerr 1, gladys 1). Known gaps: an app served through forms outside the closed list (Lambda wrappers, a string passed to uvicorn.run, a Dockerfile command) keeps only its mounted paths.

Report a problem with this pageSource: docs/adr/0015-python-module-resolution-and-fastapi-routes.md