modern-di integration for Litestar.
Full guide: Litestar integration docs
Usage example: litestar-sqlalchemy-template
Installation
uv add modern-di-litestar # or: pip install modern-di-litestar
Usage
1. Define providers
import dataclasses
from modern_di import Group, Scope, providers
@dataclasses.dataclass(kw_only=True)
class Database:
url: str
@dataclasses.dataclass(kw_only=True)
class UserRepository:
db: Database
class AppDependencies(Group):
database = providers.Factory(creator=Database, kwargs={"url": "sqlite:///app.db"})
user_repo = providers.Factory(scope=Scope.REQUEST, creator=UserRepository)
2. Wire the plugin
import litestar
from modern_di import Container
from modern_di_litestar import ModernDIPlugin
groups = [AppDependencies]
container = Container(groups=groups)
app = litestar.Litestar(
plugins=[ModernDIPlugin(container, autowired_groups=groups)],
)
container.validate() # optional fail-fast; the plugin has registered its providers by now
Passing autowired_groups to ModernDIPlugin autowires each provider as a Litestar dependency keyed by its attribute name (database, user_repo), so routes can declare them directly as parameters.
3. Inject into routes
Explicit, per route with FromDI
from modern_di_litestar import FromDI
@litestar.get(
"/users",
dependencies={"repo": FromDI(UserRepository)},
)
async def list_users(repo: UserRepository) -> list[str]: ...
FromDI accepts a type (UserRepository) or a provider instance (AppDependencies.user_repo). Pass a provider instance only for providers outside autowired_groups: Litestar rejects one provider registered under two keys and raises ImproperlyConfiguredException.
Implicit, by autowired provider name
When autowired_groups is passed to ModernDIPlugin, provider names become available as route parameters directly:
@litestar.get("/users")
async def list_users(user_repo: UserRepository) -> list[str]: ...
4. Access the raw request or websocket via DI
def request_method(request: litestar.Request) -> str:
return request.method
class AppDependencies(Group):
...
request_method = providers.Factory(
scope=Scope.REQUEST,
creator=request_method,
bound_type=None,
)
litestar_request_provider and litestar_websocket_provider are pre-built ContextProvider instances that make the current Request / WebSocket objects resolvable within DI.
5. Sub-request (action) scopes
The plugin registers a di_container dependency that yields the per-connection child container (REQUEST scope for HTTP, SESSION scope for WebSocket). For work that should live shorter than a request, build a child container from it inside a route:
@litestar.get("/")
async def handler(di_container: Container) -> None:
async with di_container.build_child_container() as action_container:
result = action_container.resolve_provider(AppDependencies.some_action_scoped_factory)
6. Retrieve the root container
from modern_di_litestar import fetch_di_container
container = fetch_di_container(app)
API
| Symbol | Description |
|---|---|
ModernDIPlugin(container, autowired_groups=None) |
Litestar InitPlugin that wires the DI container into the app lifecycle |
FromDI(dependency) |
Returns a Litestar Provide that resolves a provider (or type) per connection |
litestar_request_provider |
ContextProvider for the current litestar.Request (REQUEST scope) |
litestar_websocket_provider |
ContextProvider for the current litestar.WebSocket (SESSION scope) |
fetch_di_container(app) |
Retrieves the root container from app.state |
📦 PyPI
📝 License
Part of modern-python
Built on modern-di, a dependency-injection framework with an IoC container and scopes.
Browse the full list of templates and libraries in
modern-python; the org profile has the categorized index.
Metadata
Release files for modern-di-litestar 3.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| modern_di_litestar-3.1.0.tar.gz | 5.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| modern_di_litestar-3.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.0 kB
Release files / modern_di_litestar-3.1.0.tar.gz
| Download URL | modern_di_litestar-3.1.0.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bcf10d26a11e287b9d38ab54f1adcfa5caf3d2799ecec0d0a1265a5ad3f8cbbe
|
|
BLAKE2b-256 checksum How to use checksums |
a2a8688298849aa06716e1f5d492785b2cf4e3dfcba2abac3338cc4243595d17
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","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}
|
Release files / modern_di_litestar-3.1.0-py3-none-any.whl
| Download URL | modern_di_litestar-3.1.0-py3-none-any.whl |
|---|---|
| Size | 5.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
988cce89bf0d688f1ac02d63b444b18a19787ea96d87ab3f23f498e5843069ec
|
|
BLAKE2b-256 checksum How to use checksums |
a93d9f9969ebe700d37193215b1d46cebce913258525f593f220ec24600f8a31
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","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}
|