|
| 1 | +--- |
| 2 | +summary: modern_di_aiohttp/main.py now composes modern_di.integrations (bind for scope/context derivation — the can_prepare() provider probe stays aiohttp's own — Marker/from_di/parse_markers/resolve_markers, Container's async-with) instead of hand-rolling them; no public-API or test change. |
| 3 | +--- |
| 4 | + |
| 5 | +# Design: Adopt the modern-di integration kit |
| 6 | + |
| 7 | +## Summary |
| 8 | + |
| 9 | +`modern-di` 2.28.0 shipped `modern_di.integrations` — the framework-agnostic |
| 10 | +primitives that formalize what this package's `_di_middleware` and |
| 11 | +`@inject`/`FromDI` already hand-roll. This change swaps the hand-rolled |
| 12 | +internals for kit calls. Public API and runtime behavior are unchanged; every |
| 13 | +existing test asserts on public behavior only. |
| 14 | + |
| 15 | +## Motivation |
| 16 | + |
| 17 | +Fourth of the 13 adapter conversions surveyed by the kit's own design — see |
| 18 | +[modern-di's decision record](https://github.com/modern-python/modern-di/blob/main/planning/decisions/2026-07-13-integration-kit-shape.md). |
| 19 | +This repo is one of the two adapters the decision record calls out by name: |
| 20 | +aiohttp's `aiohttp_request_provider` and `aiohttp_websocket_provider` both |
| 21 | +bind `web.Request` (a WebSocket is an upgraded HTTP request at middleware |
| 22 | +entry, not a distinct connection type), so `integrations.classify_connection`'s |
| 23 | +isinstance-over-tuple dispatch **cannot** distinguish them — aiohttp keeps its |
| 24 | +own `can_prepare()` handshake probe to pick the provider. What the kit *does* |
| 25 | +replace is the derivation step once the provider is known: |
| 26 | +`integrations.bind(provider, connection) -> ConnectionMatch` produces the |
| 27 | +same `{web.Request: request}` context dict and scope aiohttp already |
| 28 | +hand-writes, from whichever provider `can_prepare()` selected. This mirrors |
| 29 | +the [FromDI]/`@inject` conversion already validated in |
| 30 | +[`modern-di-starlette`](https://github.com/modern-python/modern-di-starlette/pull/8) |
| 31 | +(same non-native-DI shape: `_FromDI`, `_parse_inject_params`, |
| 32 | +`_resolve_di_params` all map onto the kit byte-for-byte). |
| 33 | + |
| 34 | +## Design |
| 35 | + |
| 36 | +In `modern_di_aiohttp/main.py`: |
| 37 | + |
| 38 | +- Import `integrations` from `modern_di`. |
| 39 | +- `_di_middleware`: keep the `can_prepare()` probe to select |
| 40 | + `aiohttp_websocket_provider` or `aiohttp_request_provider`, then replace the |
| 41 | + hand-written scope/context with |
| 42 | + `match = integrations.bind(provider, request)`. Open the child via |
| 43 | + `async with fetch_di_container(request.app).build_child_container(scope=match.scope, context=match.context) as child_container:` — |
| 44 | + replacing the manual `try`/`finally: await child_container.close_async()`. |
| 45 | + `bind()` never returns `None` (unlike `classify_connection`), so no |
| 46 | + fallback branch is needed here. |
| 47 | +- Delete `_FromDI`; replace `FromDI`'s body with a direct assignment: |
| 48 | + `FromDI = integrations.from_di`. |
| 49 | +- Delete `_parse_inject_params`/`_resolve_di_params`; `inject` calls |
| 50 | + `integrations.parse_markers(func)` at decoration time and |
| 51 | + `integrations.resolve_markers(child_container, markers)` at call time. |
| 52 | +- Drop now-unused imports: `enum` (only used for the old |
| 53 | + `connection_scope: enum.IntEnum` annotation) and `dataclasses` and the |
| 54 | + `T_co` TypeVar (only `_FromDI` used them). |
| 55 | + |
| 56 | +`__init__.py`'s re-exports are untouched — every public name keeps its exact |
| 57 | +signature and behavior. No test constructs `_FromDI` directly (unlike |
| 58 | +`modern-di-litestar`'s `_Dependency` — verified by reading all four test |
| 59 | +files), so no test-file edit is expected here. |
| 60 | + |
| 61 | +## Non-goals |
| 62 | + |
| 63 | +- Any change to `setup_di`, `_on_startup`/`_on_cleanup`, or connection-provider |
| 64 | + registration — none of that is part of the kit. |
| 65 | +- The `can_prepare()` handshake probe itself — this is exactly the outlier |
| 66 | + case the kit's design deliberately does not absorb (a hypothetical seam |
| 67 | + used by only one adapter); it stays aiohttp's own code. |
| 68 | +- Any test rewrite. The existing suite asserts on public behavior only |
| 69 | + (`FromDI`, `inject`, `setup_di`, `fetch_di_container`, |
| 70 | + `fetch_request_container`, the two connection providers, and the internal |
| 71 | + `_CONTAINER_REQUEST_KEY`/`_on_startup`/`_on_cleanup` names some tests import |
| 72 | + directly — none of it references `_FromDI` or the scope-derivation |
| 73 | + internals being replaced). |
| 74 | + |
| 75 | +## Testing |
| 76 | + |
| 77 | +- `just test-ci` — 100% line coverage, all existing tests green with zero |
| 78 | + test-file changes. |
| 79 | +- `just lint-ci` — ruff (`select=ALL`), `ty check`, `check-planning`. |
| 80 | + |
| 81 | +## Risk |
| 82 | + |
| 83 | +- **Version-floor bump breaks on an environment still resolving `<2.28`** |
| 84 | + (low likelihood — semver-compatible; low impact — `pyproject.toml` pins the |
| 85 | + floor explicitly). |
| 86 | +- **`bind()` derives a different context dict than the hand-written |
| 87 | + `{web.Request: request}`** (low likelihood — `bind(provider, connection)` |
| 88 | + returns `context={provider.context_type: connection}`; both |
| 89 | + `aiohttp_request_provider` and `aiohttp_websocket_provider` have |
| 90 | + `context_type=web.Request`, so the key is identical regardless of which |
| 91 | + provider `can_prepare()` selects — verified against the provider |
| 92 | + declarations before writing this spec). |
0 commit comments