fix: raise a clear RuntimeError when inject runs without setup_di - #22
Merged
Merged
Conversation
This was referenced Sep 15, 2026
Merged
Merged
Merged
Merged
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 #13.
Problem
An
@inject-decorated dialog getter or callback reached by an update on a dispatcher that never hadsetup_dicalled failed withKeyError: 'modern_di_container'— a private key name pointing at nothing. The handler path inmain.pyhad the identical failure (aiogram's_prepare_kwargssilently drops parameters missing fromdata, so the wrapper'skwargs.popraised the sameKeyError), so this PR fixes both.Change
One private helper,
_fetch_child_container, next to_CHILD_CONTAINER_KEYinmain.py. It reads the per-update child from a mapping and convertsKeyErrorinto aRuntimeErrorthat names the cause and the fix:The handler
injectwrapper and both branches ofdialog._container_from_callgo through it. Behaviour withsetup_diin place is unchanged.Why package-local, not a
modern_diexceptionThe issue left this open.
modern-di-aiohttp(fetch_request_container) andmodern-di-starlette(inject) already raise a plainRuntimeErrorwith this same wording pattern, andmodern_di.integrationsis deliberately exception-free —ModernDIErrorcovers registration, resolution and lifecycle, not framework wiring. A shared error would need a core release and a floor bump here for no gain in this fix. Unifying across integrations is a reasonable later cross-repo change, out of scope here.Tests
Three new tests, written failing first (each reproduced the original
KeyError): handler path via a bareDispatcher, dialog getter, and dialog callback (on_start, the 2-arg branch). Coverage stays at 100%.