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.9.0.tar.gz (4.3 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.9.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: modern_di_fastapi-2.9.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.27 {"installer":{"name":"uv","version":"0.11.27","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.9.0.tar.gz
Algorithm Hash digest
SHA256 39fd664b5ececf7a9932094223da0d6228dde4e1373b6abb3fe8a991d978f016
MD5 b78d5b07607dd80789da330bdc837f09
BLAKE2b-256 3d19e7059d2d0be53fc516fe5250b423f64c5bb7a85ac9865389ef5163fd87f8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: modern_di_fastapi-2.9.0-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.27 {"installer":{"name":"uv","version":"0.11.27","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.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cd5fd9db43e06a3faf88415814e6a787d393fa428b49b3ae441eafdea32f6af9
MD5 ff76af3df06c798153c9eb0759a291ba
BLAKE2b-256 9db5d9f6703833b09f294f5d99dbda488f5dd8ba7b2f81d39f67afc56d0fc81b

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