Skip to content

fix: name setup_di and app startup when FromDI finds no request container - #55

Merged
lesnik512 merged 2 commits into
mainfrom
fix/missing-request-container-error
Sep 15, 2026
Merged

lesnik512 merged 2 commits into
mainfrom
fix/missing-request-container-error

Conversation

@lesnik512

Copy link
Copy Markdown
Member

Closes #54

What

When the DI middleware did not run for a message, Dependency.__call__ got None back from ContextRepo.get and passed it straight to the marker, so the user saw modern-di's internals fail:

AttributeError: 'NoneType' object has no attribute 'resolve_dependency'

with nothing pointing at setup_di or at app startup.

How

Dependency.__call__ checks for None and raises a RuntimeError saying the request container is missing for this message and naming the two fixes: call setup_di(app, container) on the app that owns the broker, and start the app (in tests, pair the test broker with TestApp(app) in the same async with). The message lives in a module-level constant next to the two ContextRepo keys it is about.

No behaviour changes on the happy path. When the middleware ran, the container is never None.

Tests

Two tests in tests/test_faststream_di.py, both failing on main with the raw AttributeError:

  • FromDI on an app where setup_di was never called.
  • FromDI on the fixture app entered through TestNatsBroker(broker) without TestApp(app), which after fix: install the DI middleware on every broker, on startup #52 means the startup hook that installs the middleware never ran. This is the case the modern-di docs warn about; the error now says so itself.

just lint-ci clean, just test-ci at 100% coverage.

…iner

ContextRepo.get returns None for a key nobody set, so a FromDI parameter
on a message the DI middleware never saw failed inside modern-di with
"AttributeError: 'NoneType' object has no attribute 'resolve_dependency'",
pointing at nothing in this package.

Dependency.__call__ now checks for None and raises a RuntimeError that
says the request container is missing and names the two fixes: call
setup_di on the app that owns the broker, and start the app (pair the
test broker with TestApp in tests).

Closes #54
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.

FromDI fails with an AttributeError on None when the DI middleware never ran for a message

1 participant