fix: name setup_di and app startup when FromDI finds no request container - #55
Merged
Merged
Conversation
…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
This was referenced Sep 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #54
What
When the DI middleware did not run for a message,
Dependency.__call__gotNoneback fromContextRepo.getand passed it straight to the marker, so the user saw modern-di's internals fail:with nothing pointing at
setup_dior at app startup.How
Dependency.__call__checks forNoneand raises aRuntimeErrorsaying the request container is missing for this message and naming the two fixes: callsetup_di(app, container)on the app that owns the broker, and start the app (in tests, pair the test broker withTestApp(app)in the sameasync with). The message lives in a module-level constant next to the twoContextRepokeys 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 onmainwith the rawAttributeError:FromDIon an app wheresetup_diwas never called.FromDIon the fixture app entered throughTestNatsBroker(broker)withoutTestApp(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-ciclean,just test-ciat 100% coverage.