Factory.ai

Open-Source Wikis

/

FastAPI

/

Fun facts

fastapi/fastapi

Fun facts

A handful of details about the FastAPI tree that did not fit anywhere else.

The biggest test file is over 394,000 lines

tests/test_include_router_defaults_overrides.py weighs in at 394,863 lines — eighty times the size of the file it exercises (fastapi/routing.py, 4,956 lines). It is generated to enumerate every combination of defaults that can be set when including one router inside another. The runner-up is tests/test_generate_unique_id_function.py at 76,760 lines. Together those two files are larger than the rest of the test suite combined.

The framework imports more from Starlette than from itself

Counting from starlette and from fastapi imports in fastapi/applications.py, Starlette wins. Most of FastAPI.__init__ consists of forwarding parameters to Starlette.__init__, and many module-level objects — JSONResponse, HTMLResponse, Request, WebSocket, Mount, the middleware classes — are either used as-is or re-exported. The repository is, in effect, a typed dependency-injection layer over Starlette.

_compat exists because Pydantic v1 was once supported

fastapi/_compat/__init__.py re-exports a stable surface of names. The implementations are now exclusively in fastapi/_compat/v2.py, but the directory layout reveals how the compatibility shim used to host parallel v1 and v2 modules. Today, attempting to use a Pydantic v1 model raises PydanticV1NotSupportedError from fastapi/utils.py:create_model_field. The _compat boundary survives as Pydantic-v2 abstraction, not as a v1/v2 switch.

Three exit stacks per request

Every request lives inside three nested AsyncExitStacks:

  • scope["fastapi_middleware_astack"] — created by AsyncExitStackMiddleware in fastapi/middleware/asyncexitstack.py, mainly used to close uploaded files.
  • scope["fastapi_inner_astack"] — created in request_response() in fastapi/routing.py, owns request-scoped yield-style dependencies.
  • scope["fastapi_function_astack"] — also in request_response(), owns function-scoped yield dependencies.

That layered structure is what lets Depends(...) callables yield and have their cleanup blocks run after the response, without surprising async edge cases.

The fastapi command is not in this repo

fastapi/cli.py is five lines long. The actual CLI lives in the separate fastapi-cli package, pulled in by the standard extra in pyproject.toml. If you pip install fastapi without extras and run fastapi, the shim raises a RuntimeError telling you to install fastapi[standard].

A doc page is also a test fixture

Every example under docs_src/ is imported by a corresponding test in tests/test_tutorial/. The pyproject's coverage.run.source lists docs_src next to tests and fastapi for that reason. Editing a tutorial example will, by design, fail tests if it changes the example's response shape.

The framework is one class deep

fastapi.FastAPI inherits from starlette.applications.Starlette. fastapi.APIRouter inherits from starlette.routing.Router. fastapi.HTTPException inherits from starlette.exceptions.HTTPException. fastapi.UploadFile inherits from starlette.datastructures.UploadFile. fastapi.BackgroundTasks inherits from starlette.background.BackgroundTasks. The entire user-facing API is a one-class-deep Starlette delta.

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

Fun facts – FastAPI wiki | Factory