Skip to content

fix: raise a clear RuntimeError when action_scope runs outside @inject - #49

Merged
lesnik512 merged 1 commit into
mainfrom
fix/clear-error-action-scope-without-inject
Sep 15, 2026
Merged

lesnik512 merged 1 commit into
mainfrom
fix/clear-error-action-scope-without-inject

Conversation

@lesnik512

Copy link
Copy Markdown
Member

Problem

Calling action_scope(ctx) from a command that is not decorated with @inject failed inside the ctx.meta lookup with an error that names a private key and nothing the user can act on:

KeyError: 'modern_di_typer.command_container'

Verified empirically with CliRunner before the change.

Change

action_scope catches KeyError on the ctx.meta[_COMMAND_CONTAINER_KEY] lookup only and raises:

RuntimeError: No modern-di command container found for this command. Decorate the command with @inject so action_scope has a per-command container to nest under.

with from None, so the traceback ends at the actionable message. Behaviour inside an @inject command is unchanged. The README action_scope bullet notes the new error.

This completes the sweep started by #47, following the guide rule in modern-python/modern-di#490: a plain package-local RuntimeError, not a ModernDIError subclass.

Tests

Written red first in tests/test_commands.py:

  • test_action_scope_without_inject_raises_clear_error: Typer app with setup_di, command without @inject that opens action_scope(ctx); failed with the original KeyError: 'modern_di_typer.command_container' before the fix

Asserts RuntimeError matching @inject. just lint and just test-ci pass at 100% coverage.

@lesnik512
lesnik512 merged commit 4ff3624 into main Sep 15, 2026
8 checks passed
@lesnik512
lesnik512 deleted the fix/clear-error-action-scope-without-inject branch September 15, 2026 17:24
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