Skip to content

fix: raise a clear RuntimeError when inject runs without setup_di - #22

Merged
lesnik512 merged 1 commit into
mainfrom
fix/clear-error-without-setup-di
Sep 15, 2026
Merged

lesnik512 merged 1 commit into
mainfrom
fix/clear-error-without-setup-di

Conversation

@lesnik512

Copy link
Copy Markdown
Member

Closes #13.

Problem

An @inject-decorated dialog getter or callback reached by an update on a dispatcher that never had setup_di called failed with KeyError: 'modern_di_container' — a private key name pointing at nothing. The handler path in main.py had the identical failure (aiogram's _prepare_kwargs silently drops parameters missing from data, so the wrapper's kwargs.pop raised the same KeyError), so this PR fixes both.

Change

One private helper, _fetch_child_container, next to _CHILD_CONTAINER_KEY in main.py. It reads the per-update child from a mapping and converts KeyError into a RuntimeError that names the cause and the fix:

No modern-di container found for this update. Call setup_di(dispatcher, container) so updates pass through the modern-di middleware before using @Inject.

The handler inject wrapper and both branches of dialog._container_from_call go through it. Behaviour with setup_di in place is unchanged.

Why package-local, not a modern_di exception

The issue left this open. modern-di-aiohttp (fetch_request_container) and modern-di-starlette (inject) already raise a plain RuntimeError with this same wording pattern, and modern_di.integrations is deliberately exception-free — ModernDIError covers 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 bare Dispatcher, dialog getter, and dialog callback (on_start, the 2-arg branch). Coverage stays at 100%.

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.

Raise a clear error when dialog DI is used without setup_di

1 participant