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

@codecov

codecov Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.89%. Comparing base (5daed12) to head (8e6f24b).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #324      +/-   ##
==========================================
+ Coverage   96.83%   96.89%   +0.05%     
==========================================
  Files           1        1              
  Lines         601      612      +11     
  Branches      103       94       -9     
==========================================
+ Hits          582      593      +11     
  Misses         13       13              
  Partials        6        6              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@davidparsson

Copy link
Copy Markdown
Collaborator

Thanks for your contribution! I think this is something Injector should support. I'll just think one round about the implementation.

@davidparsson

Copy link
Copy Markdown
Collaborator

If you're able to, please fix CI with a bit more coverage, and resolve the conflicts (sorry!).

@DYNOSuprovo

Copy link
Copy Markdown
Author

Thanks for the review and feedback @davidparsson! Glad to hear this aligns with Injector's goals. I will resolve the merge conflicts and add more test cases to increase CI coverage.

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>
@DYNOSuprovo
DYNOSuprovo force-pushed the fix-child-injector-multibind branch from cd58440 to 8e6f24b Compare October 7, 2026 11:44
@DYNOSuprovo

Copy link
Copy Markdown
Author

Rebased cleanly onto latest \master, resolved the conflict in \CHANGES, and added test coverage for edge cases (including child multibinds when the parent has no binding for that interface, and child multibinds when the parent has a non-multibinder binding). All 140 tests pass with full 100% branch and statement coverage on the new lines.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Inherited scoped-provider errors can be swallowed, silently returning incomplete collections.

1 open finding
What changed in this PR

Extends inherited multibindings in child injectors, addressing #297 while preserving parent isolation.

Changes:

  • Combines parent and child contributions, with parent-first lists and child-overridden dictionary entries.
  • Adds hierarchy and fallback tests.
  • Documents the behavior change.
File Description
injector/​__init__.py Resolves inherited multibinding providers before local providers.
injector_test.py Tests inheritance, overrides, skipped levels, and fallback behavior.
CHANGES Announces inherited multibinding support.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread injector/__init__.py
Comment on lines +354 to +359
try:
parent_binding, _ = self._owner_binder.parent._get_binding(self._interface)
if isinstance(parent_binding.provider, MultiBinder):
yield from parent_binding.provider.get_scoped_providers(injector)
except KeyError:
pass
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

3 participants