Skip to content

Extend parent injector multibindings in child injectors - #324

Open
DYNOSuprovo wants to merge 1 commit into
python-injector:masterfrom
DYNOSuprovo:fix-child-injector-multibind
Open

DYNOSuprovo wants to merge 1 commit into
python-injector:masterfrom
DYNOSuprovo:fix-child-injector-multibind

Conversation

@DYNOSuprovo

Copy link
Copy Markdown

Summary

Fixes #297.

Previously, when a child injector configured multibindings on an interface (such as List[T] or Dict[K, V]), a new MultiBinder was instantiated without reference to the parent injector's multibinder. Consequently, child_injector.get() only retrieved the multibindings defined on the child, completely shadowing the parent's multibindings.

This change:

  1. Keeps a reference to the owner Binder and interface on MultiBinder.
  2. In MultiBinder.get_scoped_providers(), checks if the owner binder has a parent binder with a registered MultiBinder for the same interface, yielding scoped providers from the parent first before yielding child providers.
  3. For lists, items from the parent appear first followed by items from the child.
  4. For dicts, entries from the parent are included and can be extended or overridden by the child.
  5. Supports multiple levels of injector hierarchy (grandchild injectors, skipped levels).
  6. Preserves isolation so parent injectors remain unaffected by child multibindings.

Tests

Added test cases in injector_test.py:

  • test_multibinds_are_extended_by_child_injectors
  • test_multibind_dict_is_extended_by_child_injectors
  • test_multibind_multi_level_hierarchy_extended_by_child_injectors
  • test_multibind_skipped_level_child_injector

Allow child injectors to inherit and extend multibindings registered on their parent injectors for both sequence and mapping multibinds.

Fixes python-injector#297

Signed-off-by: DYNOSuprovo <DYNOSuprovo@users.noreply.github.com>
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.

Child injectors should respect parent injector's multibinds

1 participant