3.0 KiB
CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Build and Development Commands
Install dependencies:
pip install -e ".[all]"
pip install -r requirements-tests.txt
Run tests:
# Run all tests with coverage
./scripts/test.sh
# Run a single test file
coverage run -m pytest tests/test_application.py
# Run a specific test
coverage run -m pytest tests/test_application.py::test_app -v
# Note: PYTHONPATH=./docs_src is set by the test script for importing tutorial examples
Linting and formatting:
# Check linting (mypy + ruff)
./scripts/lint.sh
# Auto-fix and format
./scripts/format.sh
# Individual commands
mypy fastapi
ruff check fastapi tests docs_src scripts
ruff format fastapi tests
Pre-commit hooks: Uses uv run for ruff and docs generation hooks.
Architecture Overview
FastAPI is built on top of Starlette (ASGI framework) and Pydantic (data validation).
Core Module Structure (fastapi/)
-
applications.py-FastAPIclass extending Starlette'sStarletteclass. Contains app initialization, OpenAPI schema generation, and route registration methods (get,post,put, etc.) -
routing.py-APIRouterandAPIRouteclasses. Handles path operation decoration, request/response processing, and dependency injection integration. Therequest_responsefunction wraps endpoints withAsyncExitStackfor dependency lifecycle management. -
params.py- Parameter classes (Query,Path,Header,Cookie,Body,Form,File) extending Pydantic'sFieldInfo. These define how request data is extracted and validated. -
param_functions.py- Function versions of parameter classes for use withAnnotatedtyping pattern. -
dependencies/- Dependency injection system:models.py-Dependantdataclass representing resolved dependency treeutils.py-get_dependant()analyzes function signatures,solve_dependencies()resolves and caches dependencies at request time
-
openapi/- OpenAPI/Swagger generation:utils.py-get_openapi()generates OpenAPI schema from routesdocs.py- Swagger UI and ReDoc HTML generationmodels.py- Pydantic models for OpenAPI spec
-
security/- Security scheme implementations (OAuth2, API Key, HTTP Basic/Bearer) -
_compat/- Pydantic v2 compatibility layer
Request Flow
- Request hits
FastAPIapp (Starlette ASGI) APIRoute.handle()matches route and calls endpoint wrappersolve_dependencies()resolves dependency tree (with caching)- Parameters extracted from path/query/headers/body via
Paramclasses - Pydantic validates all inputs
- Endpoint function called with resolved dependencies
- Response serialized through
jsonable_encoder
Testing Patterns
Tests use TestClient from Starlette (via fastapi.testclient). Test files in tests/ mirror the structure of tutorials in docs_src/. Tests import example code from docs_src/ to verify documentation examples work correctly.