Skip to content

Releases: modern-python/modern-di-aiohttp

Release list

2.0.0

Choose a tag to compare

@github-actions github-actions released this 02 Jul 11:48
5cb5470

modern-di-aiohttp 2.0.0 — first release

First release of the aiohttp integration for modern-di. It wires a
modern-di container into an aiohttp
web.Application and resolves dependencies into handlers. The version starts
at 2.0.0 to track the modern-di 2.x ecosystem alongside the other official
integrations.

aiohttp has no dependency-injection system of its own, so this integration
follows modern-di's decorator path: middleware owns the per-connection
container lifecycle, and an @inject decorator with FromDI markers resolves
dependencies.

Feature

  • setup_di(app, container). Stashes the root container on the app
    (web.AppKey("modern_di_container", Container), read back with
    fetch_di_container(app)), registers the connection providers, reopens the
    container on on_startup (so repeated startup/cleanup cycles work), closes
    it on on_cleanup, and installs the DI middleware. Returns the container.
  • Per-connection child containers. Middleware detects a WebSocket upgrade
    via web.WebSocketResponse().can_prepare(request).ok and opens a
    Scope.REQUEST child container for each HTTP request or a Scope.SESSION
    child for each WebSocket connection, injecting the connection as context and
    closing the child when the connection ends. A handler reads its child
    container with fetch_request_container(request). Per-message work inside a
    WebSocket handler can open a nested Scope.REQUEST child of the SESSION
    container.
  • FromDI + @inject. Mark a handler parameter with
    Annotated[T, FromDI(provider_or_type)] and decorate the handler with
    @inject; the decorator resolves each marked parameter from the
    connection's child container. A clear RuntimeError is raised if @inject
    runs without setup_di.
  • aiohttp_request_provider / aiohttp_websocket_provider. Connection
    providers exposing the web.Request: the former is registered by type at
    Scope.REQUEST for HTTP handlers; the latter is reference-only
    (bound_type=None) at Scope.SESSION for WebSocket handlers, injected via
    FromDI(aiohttp_websocket_provider).

Packaging

  • Requires aiohttp>=3.9,<4 and modern-di>=2.21.0,<3; Python 3.10–3.14.
  • Ships py.typed; zero runtime dependencies beyond aiohttp and modern-di.

Downstream

No action needed — this is a new package.

Internals

  • 100% line coverage; ruff, ty, and eof-fixer clean across Python
    3.10–3.14.
  • Uses the portable planning convention (planning/) with an architecture/
    truth home; releases are tag-driven.