Skip to main content

modern-di-aiohttp

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

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

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

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: modern_di_aiohttp-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_aiohttp-3.1.1.tar.gz
Algorithm Hash digest
SHA256 39c9641e0037f4f930b270876e6db4f1241b22a88ba00433b45e9e09eb9ba84c
MD5 10806f6462c2a9dbe16ccd67f60031c3
BLAKE2b-256 1ada73575fdcc03662df7357b69a787eb87a64f94472af593579e5f2b07d2a8e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: modern_di_aiohttp-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_aiohttp-3.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 547b102ebf39f2eea0cd1f762b525578a5219b828620073b30cfb28ee12157ad
MD5 e9d8a0f690c6432643ebc542831da0da
BLAKE2b-256 a59fa35ecea6209ecab1b11ace2bbdc05a745757cb92378f536e6191fcaa5cec

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