Skip to main content

modern-di integration for FastAPI

Project description

modern-di-fastapi

PyPI version Supported Python versions Downloads Coverage CI License GitHub stars uv Ruff ty

Modern-DI integration for FastAPI.

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-request 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], validate=True)
setup_di(app, container)


@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 appends a lifespan that closes it on shutdown (merges with any existing lifespan=)
FromDI(provider, *, use_cache=True) FastAPI Depends that resolves a provider (or type) from the per-request child container
fetch_di_container(app) Returns the app-scoped container from app.state
build_di_container(connection) FastAPI Depends callable that yields the per-request 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

modern_di_fastapi-2.8.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

modern_di_fastapi-2.8.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file modern_di_fastapi-2.8.0.tar.gz.

File metadata

  • Download URL: modern_di_fastapi-2.8.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","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}

File hashes

Hashes for modern_di_fastapi-2.8.0.tar.gz
Algorithm Hash digest
SHA256 1eb11aaa2422c8617dba4c05db544a345b41bf53e15c76de73a25989fd5e5d33
MD5 60d1bf9467069d9d2bb6bb2f2e5a5550
BLAKE2b-256 8c61dc5da650889ffd7cf72343f18b90e91e5be8bee7191b8dc9f0604ffd1727

See more details on using hashes here.

File details

Details for the file modern_di_fastapi-2.8.0-py3-none-any.whl.

File metadata

  • Download URL: modern_di_fastapi-2.8.0-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","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}

File hashes

Hashes for modern_di_fastapi-2.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fde22ef6cbb7a135eaea03c2f77885adba5e2220df3e72900c904e2948d2fba5
MD5 3d4a8e7fe2f161bbdd2deb13b4189d82
BLAKE2b-256 46276b699cb3f94ae1fa13ed4adeaf1eea5a8c1268063f9cda30ee29ce104f5c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page