Modern-DI integration for aiohttp.
Full guide: aiohttp integration docs
Usage example: examples/
Installation
uv add modern-di-aiohttp # or: pip install modern-di-aiohttp
Usage
aiohttp has no dependency-injection system of its own, so modern-di-aiohttp pairs an @inject decorator with inert FromDI markers. setup_di stores the container on the app, opens it on startup and closes it on cleanup, and installs middleware that builds a per-connection child container automatically; FromDI resolves a provider (or type) into a handler parameter.
import dataclasses
import typing
from aiohttp import web
from modern_di import Container, Group, Scope, providers
from modern_di_aiohttp import FromDI, inject, 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)
@inject
async def index(
request: web.Request,
user_service: typing.Annotated[UserService, FromDI(Dependencies.user_service)],
) -> web.Response:
return web.json_response({"debug": user_service.settings.debug})
app = web.Application()
app.router.add_get("/", index)
container = Container(groups=[Dependencies])
setup_di(app, container)
container.validate() # optional fail-fast; must come after setup_di registers its providers
@inject works the same on the methods of a class-based web.View. Decorate the handler method, not the class; aiohttp calls the method with no arguments, so the request is taken from self.request:
class Users(web.View):
@inject
async def get(
self,
user_service: typing.Annotated[UserService, FromDI(Dependencies.user_service)],
) -> web.Response:
return web.json_response({"debug": user_service.settings.debug})
app.router.add_view("/users", Users)
An HTTP request opens a Scope.REQUEST child container; a WebSocket connection opens a Scope.SESSION one. The connection aiohttp.web.Request is resolvable within DI: HTTP handlers and REQUEST-scoped factories inject it by type via aiohttp_request_provider, while WebSocket handlers read it via FromDI(aiohttp_websocket_provider). For per-message work inside a WebSocket handler, open a nested Scope.REQUEST child of the session container fetched with fetch_request_container.
API
| Symbol | Description |
|---|---|
setup_di(app, container) |
Stores the container on the app, wires on_startup/on_cleanup (reopen on startup, close on cleanup), registers the connection providers, and installs the per-connection middleware |
inject(handler) |
Decorator that resolves every FromDI-marked parameter from the request's child container and passes them to the handler; works on a function handler and on a method of a web.View subclass |
FromDI(dependency) |
Inert Annotated marker resolved by @inject; accepts a provider or a type |
fetch_di_container(app) |
Returns the app-scoped root container |
fetch_request_container(request) |
Returns the per-connection child container the middleware built for this request |
aiohttp_request_provider |
ContextProvider for the current aiohttp.web.Request (REQUEST scope) |
aiohttp_websocket_provider |
ContextProvider for the connection aiohttp.web.Request at WebSocket SESSION scope |
📦 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.
Metadata
Release files for modern-di-aiohttp 3.2.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_aiohttp-3.2.0.tar.gz | 5.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| modern_di_aiohttp-3.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.4 kB
Release files / modern_di_aiohttp-3.2.0.tar.gz
| Download URL | modern_di_aiohttp-3.2.0.tar.gz |
|---|---|
| Size | 5.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4792f877ef0f5e588de321e8efd327e5c2b86f91de152cbda5f1f7d3ff2ed0e4
|
|
BLAKE2b-256 checksum How to use checksums |
c1bb5326fd7e3750fa4c996c8dd845a596978eb5b8be9699be6c323ac7e49310
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.15 {"installer":{"name":"uv","version":"0.12.15","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_aiohttp-3.2.0-py3-none-any.whl
| Download URL | modern_di_aiohttp-3.2.0-py3-none-any.whl |
|---|---|
| Size | 5.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
478cfa4af9c0808ccc234b744ec754799770dd381980ac6d02b95d44aa9c4728
|
|
BLAKE2b-256 checksum How to use checksums |
87313bc390a8e7ff58c7b3d88daed46dcbcea8401f29aaa7fe376b76cd648e18
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.15 {"installer":{"name":"uv","version":"0.12.15","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}
|