Skip to main content

modern-di-starlette

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

Modern-DI integration for Starlette.

Full guide: Starlette integration docs

Usage example: examples/

Installation

uv add modern-di-starlette      # or: pip install modern-di-starlette

Usage

setup_di registers the container, composes the lifespan, and installs middleware that builds a per-connection child container. Decorate an endpoint with @inject and mark parameters with FromDI to receive resolved dependencies. Starlette has no native DI, so @inject is required (there is no Depends).

import dataclasses
import typing

from modern_di import Container, Group, Scope, providers
from modern_di_starlette import FromDI, inject, setup_di
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import JSONResponse
from starlette.routing import Route


@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)


@inject
async def homepage(
    request: Request,
    service: typing.Annotated[UserService, FromDI(Dependencies.user_service)],
) -> JSONResponse:
    return JSONResponse({"debug": service.settings.debug})


app = Starlette(routes=[Route("/", homepage)])
container = Container(groups=[Dependencies])
setup_di(app, container)
container.validate()  # optional fail-fast; must come after setup_di registers its providers

An HTTP request opens a Scope.REQUEST child container; a WebSocket connection opens a Scope.SESSION one, both built by the middleware before your handler runs. The connection starlette.requests.Request / starlette.websockets.WebSocket are resolvable within DI via the pre-built starlette_request_provider / starlette_websocket_provider context providers.

API

Symbol Description
setup_di(app, container) Registers the container on app.state, composes the lifespan (opens/closes the container), and installs the middleware that builds a per-connection child container; returns the container
FromDI(dependency) Inert marker (used with @inject) that resolves a provider or type from the per-connection child container
inject(handler) Decorator for an async def handler taking a Request or WebSocket; resolves its FromDI-annotated parameters
fetch_di_container(app) Returns the root Container stored on app.state
starlette_request_provider ContextProvider for starlette.requests.Request (REQUEST scope), auto-registered
starlette_websocket_provider ContextProvider for starlette.websockets.WebSocket (SESSION scope), auto-registered

📦 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.

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

Uploaded Python 3

File details

Details for the file modern_di_starlette-3.1.1.tar.gz.

File metadata

  • Download URL: modern_di_starlette-3.1.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.33 {"installer":{"name":"uv","version":"0.11.33","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_starlette-3.1.1.tar.gz
Algorithm Hash digest
SHA256 1c3295b4b489791bb2a453840c8b8c6be613b96d8811bc075cf3b238c2fac96b
MD5 f9887727563fca60ead75d9f8ef066ae
BLAKE2b-256 89092964b2dc50e189028ef60801098a51a87fc44c85248a351a15f3690f88c7

See more details on using hashes here.

File details

Details for the file modern_di_starlette-3.1.1-py3-none-any.whl.

File metadata

  • Download URL: modern_di_starlette-3.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.33 {"installer":{"name":"uv","version":"0.11.33","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_starlette-3.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2a726d915c82193ff40f142489cd15d95fbde6f250b34bbd3da59d2b7bde5e42
MD5 7fa89bd3160b5e48a8d7be8d7165df5f
BLAKE2b-256 21fdc94535e0d691acc6f091e2a9b6d3d55bf25c53d3301ba5e104b9a6789291

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 Sentry Error logging StatusPage Status page