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

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: modern_di_fastapi-2.7.3.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","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.7.3.tar.gz
Algorithm Hash digest
SHA256 d985efdc01dc940af98f45d2b6882a8cbd8dcd6d2c67dd83b831430b7e69d108
MD5 690520688953c2b2f4bb8492c36ce15c
BLAKE2b-256 a2366ce30c98c8aab47ccbfbcb4c9b762d453e0690755392994575ac43a0dc97

See more details on using hashes here.

File details

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

File metadata

  • Download URL: modern_di_fastapi-2.7.3-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","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.7.3-py3-none-any.whl
Algorithm Hash digest
SHA256 499319ccda1d26ad9bfb458ef84d9a8eac05c04a50434d360a57dcf96c8106e3
MD5 32cc624fa3bccd649110164ff4ec1f3a
BLAKE2b-256 93d569ad5cefe6a20da36cd31f06de44780a2456e85a30fef68af1baaf3b1dc4

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