modern-di integration for Starlette
Project description
Modern-DI integration for Starlette.
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], validate=True)
setup_di(app, container)
Part of modern-python
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file modern_di_starlette-2.0.0.tar.gz.
File metadata
- Download URL: modern_di_starlette-2.0.0.tar.gz
- Upload date:
- Size: 4.7 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b7971b05ec74f395bc7fb58aeaf98c12aacff5dc1278c2b446a7a0e0c31d55a2
|
|
| MD5 |
382b54b43945597d9198b292fa10f858
|
|
| BLAKE2b-256 |
c4feff91154d330cb755e524e20b7bba09aa50aa96469bf3a4a15e3da658aec5
|
File details
Details for the file modern_di_starlette-2.0.0-py3-none-any.whl.
File metadata
- Download URL: modern_di_starlette-2.0.0-py3-none-any.whl
- Upload date:
- Size: 5.3 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1ee3a6bb8777259447aab8f776e1810de0b550a4a35cd7efa90f6a85441bdbcb
|
|
| MD5 |
77f743acf722e5bbf6c8ec9595a33b88
|
|
| BLAKE2b-256 |
3754f4f4145006dbfcd28adddfe014ec1ec93f3608be28ccbde7dd9a050a37d8
|