Skip to content

refactor: single-source the connection-kind mapping off the providers#19

Merged
lesnik512 merged 1 commit into
mainfrom
refactor/single-source-connection-mapping
Jun 26, 2026
Merged

refactor: single-source the connection-kind mapping off the providers#19
lesnik512 merged 1 commit into
mainfrom
refactor/single-source-connection-mapping

Conversation

@lesnik512

Copy link
Copy Markdown
Member

Completes candidate 1 from the architecture review (and finishes the single-sourcing #15 began). Behavior is unchanged.

What

build_di_container re-stated, in an isinstance ladder, the connection types and context keys that the two ContextProviders already carry — the connection-kind concept lived in two places.

modern-di 2.21.0 exposes ContextProvider.context_type (modern-python/modern-di#244, shipped 2.21.0), so the registered providers can now be the single source:

_CONNECTION_PROVIDERS = (fastapi_request_provider, fastapi_websocket_provider)

# setup_di
container.providers_registry.add_providers(*_CONNECTION_PROVIDERS)

# build_di_container
for provider in _CONNECTION_PROVIDERS:
    if isinstance(connection, provider.context_type):
        context[provider.context_type] = connection
        scope = provider.scope
        break

Adding a connection kind is now adding a provider to _CONNECTION_PROVIDERS; the dispatch and registration both flow from it.

Wins (in the review's terms)

  • locality: connection-kind knowledge in one place
  • leverage: one mapping drives both registration and dispatch
  • interface unchanged; the isinstance ladder is absorbed

Changes

  • modern_di_fastapi/main.py — the refactor above.
  • pyproject.toml — floor bump modern-di>=2.21.0,<3 (needs the public context_type).
  • architecture/container-lifecycle.md + architecture/glossary.md — promoted per the planning convention (the Scope mapping is now described as provider-sourced).

Verification

  • just test-ci100% coverage, 7 passed (existing Request + WebSocket tests cover the dispatch).
  • just lint-ci → ruff / ty / eof / check-planning all clean.

🤖 Generated with Claude Code

build_di_container re-stated, via an isinstance ladder, the connection types
and context keys that the context providers already carry. With modern-di 2.21.0
exposing ContextProvider.context_type, the registered providers are now the
single source: setup_di registers them and build_di_container walks them, taking
context_type and scope from each. Adding a connection kind is adding a provider;
this dispatch no longer changes. Behavior is unchanged — existing Request and
WebSocket tests cover it at 100%.

Promotes the change into architecture/container-lifecycle.md and sharpens the
glossary's Scope mapping entry, per the planning convention.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@lesnik512 lesnik512 merged commit 5056b4e into main Jun 26, 2026
6 checks passed
@lesnik512 lesnik512 deleted the refactor/single-source-connection-mapping branch June 26, 2026 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant