Skip to content

refactor: name the container context keys#19

Merged
lesnik512 merged 1 commit into
mainfrom
chore/name-container-context-keys
Jun 25, 2026
Merged

refactor: name the container context keys#19
lesnik512 merged 1 commit into
mainfrom
chore/name-container-context-keys

Conversation

@lesnik512

Copy link
Copy Markdown
Member

What

Replace the four bare ContextRepo string literals with two module-level constants:

_ROOT_CONTAINER_KEY = "di_container"
_REQUEST_CONTAINER_KEY = "request_container"

Why

The root and per-message containers are handed off through FastStream's ContextRepo under string keys — each written in one place, read in another:

key writer reader
"di_container" setup_di fetch_di_container
"request_container" _DiMiddleware.consume_scope Dependency.__call__

Writer and reader had to agree on the literal with nothing enforcing it — this handoff is a seam, but an invisible one carried by duplicated strings. A rename or typo on either end failed silently at runtime, not at lint/type-check time. Naming the keys gives the seam one definition: writer and reader provably reference the same name, and a mismatch is a NameError at import.

This is a locality win, not new behaviour. Values are unchanged.

Verification

  • just lint clean (ruff format + ruff check + ty).
  • just test — 5 passed, main.py at 100% coverage. Every key site is exercised by the existing NATS integration round-trips, so no new test is needed.

🤖 Generated with Claude Code

The root and per-message containers are handed off through FastStream's
ContextRepo under bare string keys, each written in one place and read in
another:

  "di_container"      setup_di (write) -> fetch_di_container (read)
  "request_container" _DiMiddleware.consume_scope (write) -> Dependency (read)

Writer and reader had to agree on the literal with nothing enforcing it; a
rename or typo on either end failed only at runtime. Promote both to
module-level constants (_ROOT_CONTAINER_KEY, _REQUEST_CONTAINER_KEY) so the
handoff has one definition and a mismatch is a NameError at import.

Values unchanged; behaviour-preserving. Existing integration round-trips
cover every site at 100%.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@lesnik512 lesnik512 merged commit 188eaf3 into main Jun 25, 2026
6 checks passed
@lesnik512 lesnik512 deleted the chore/name-container-context-keys branch June 25, 2026 20:06
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