Modern-DI integration for FastAPI.
Full guide: FastAPI integration docs
Usage example: fastapi-sqlalchemy-template
Installation
uv add modern-di-fastapi # or: pip install modern-di-fastapi
Usage
setup_di registers the container and builds a per-connection child container automatically; FromDI resolves a provider (or type) into a route parameter.
import dataclasses
import fastapi
from modern_di import Container, Group, Scope, providers
from modern_di_fastapi import FromDI, setup_di
@dataclasses.dataclass(kw_only=True)
class Settings:
debug: bool = True
@dataclasses.dataclass(kw_only=True)
class UserService:
settings: Settings # auto-injected by type
class Dependencies(Group):
settings = providers.Factory(scope=Scope.APP, creator=Settings)
user_service = providers.Factory(scope=Scope.REQUEST, creator=UserService)
app = fastapi.FastAPI()
container = Container(groups=[Dependencies])
setup_di(app, container)
container.validate() # optional fail-fast; must come after setup_di registers its providers
@app.get("/")
async def index(user_service: UserService = FromDI(Dependencies.user_service)) -> dict[str, bool]:
return {"debug": user_service.settings.debug}
The framework Request / WebSocket are resolvable within DI via the pre-built fastapi_request_provider / fastapi_websocket_provider context providers.
API
| Symbol | Description |
|---|---|
setup_di(app, container) |
Stores the container on app.state and composes the container's open/close into the app lifespan (nesting inside any existing lifespan=) |
FromDI(provider, *, use_cache=True) |
FastAPI Depends that resolves a provider (or type) from the per-connection child container. Raises RuntimeError naming setup_di when a request reaches it without setup_di called |
fetch_di_container(app) |
Returns the app-scoped container from app.state. Raises RuntimeError naming setup_di when called without setup_di called |
build_di_container(connection) |
FastAPI Depends callable that yields the per-connection child container — REQUEST scope for an HTTP request, SESSION scope for a WebSocket |
fastapi_request_provider |
ContextProvider for the current fastapi.Request |
fastapi_websocket_provider |
ContextProvider for the current fastapi.WebSocket |
📦 PyPI
📝 License
Part of modern-python
Built on modern-di, a dependency-injection framework with IoC container and scopes.
Browse the full list of templates and libraries in
modern-python — see the org profile for the categorized index.
Metadata
Release files for modern-di-fastapi 3.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| modern_di_fastapi-3.0.2.tar.gz | 4.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| modern_di_fastapi-3.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.8 kB
Release files / modern_di_fastapi-3.0.2.tar.gz
| Download URL | modern_di_fastapi-3.0.2.tar.gz |
|---|---|
| Size | 4.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d69e96a534d18a25845084cc59c85ad5f6cbf044edde134739d0ea8e326821dc
|
|
BLAKE2b-256 checksum How to use checksums |
ec298113db8ab40501f99306da88d683ac13a572f3399f4a26f03bfe774fb374
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.15 {"installer":{"name":"uv","version":"0.12.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / modern_di_fastapi-3.0.2-py3-none-any.whl
| Download URL | modern_di_fastapi-3.0.2-py3-none-any.whl |
|---|---|
| Size | 5.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0d46f853ad244388529aed3bd4b95a59fb516797b800fbc469ebc3311c39e203
|
|
BLAKE2b-256 checksum How to use checksums |
b7f3a83a1c98d40ef3e51636f0ed565de1cd4fda15dfc07a30a638e4df0a00e8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.15 {"installer":{"name":"uv","version":"0.12.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|