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

Uploaded Python 3

File details

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

File metadata

  • Download URL: modern_di_fastapi-2.8.1.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","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.1.tar.gz
Algorithm Hash digest
SHA256 cd06bad04b20cb4eeba37435598d9550aca1ac8651ca370d773b5aa046d8ba0b
MD5 1667d3baecc4184af92cac2ec3e109b5
BLAKE2b-256 4f662fee64fabfd0bfe5e1d30dc072cd56b72a096cd2787d5664faa7e5208500

See more details on using hashes here.

File details

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

File metadata

  • Download URL: modern_di_fastapi-2.8.1-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5f0bc6b109a5489b0d201501e082a41e051dfaf0b4aebf1281b4b140f34c79f2
MD5 d693639e4e2d2d41b81d862bb8b7c285
BLAKE2b-256 f15c4cedfc420604e56a51bfe427e81de80ea888063b7894711427ee60d80de0

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