Skip to content

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

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

lesnik512 merged 2 commits into
mainfrom
fix/clear-error-without-setup-di

Conversation

@lesnik512

Copy link
Copy Markdown
Member

Problem

An @inject-decorated arq task that runs on a worker where setup_di(worker_settings, container) was never called dies inside the wrapper with:

KeyError: 'modern_di_request_container'

That is a private key name pointing at nothing; it says neither what is missing nor how to fix it.

Change

inject's wrapper now reads the per-job child through a private _fetch_child_container(ctx) helper that catches the KeyError and raises a plain RuntimeError with from None:

No modern-di container found for this job. Call setup_di(worker_settings, container) so jobs pass through the modern-di hooks before using @inject.

Only the lookup is wrapped; behaviour with setup_di in place is unchanged.

This follows the aiohttp/starlette/aiogram integrations (modern-python/modern-di-aiogram#22) and the rule in the integration guide, modern-python/modern-di#490: a package-local RuntimeError naming the fix, not a ModernDIError subclass.

Tests

  • tests/test_jobs.py::test_inject_without_setup_di_names_the_fix: calls an @inject task with a bare ctx dict (no setup_di) and asserts RuntimeError matching setup_di(worker_settings, container). Written first; failed with the original KeyError before the fix.
  • just lint and just test-ci pass (coverage 100%).

@lesnik512
lesnik512 merged commit ca4c24a into main Sep 15, 2026
7 checks passed
@lesnik512
lesnik512 deleted the fix/clear-error-without-setup-di branch September 15, 2026 17:10
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