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

❌ Patch coverage is 78.57143% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.40%. Comparing base (d8f707d) to head (cd58440).

Files with missing lines Patch % Lines
injector/__init__.py 78.57% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #324      +/-   ##
==========================================
- Coverage   96.83%   96.40%   -0.44%     
==========================================
  Files           1        1              
  Lines         601      612      +11     
  Branches      103      105       +2     
==========================================
+ Hits          582      590       +8     
- Misses         13       15       +2     
- Partials        6        7       +1     

☔ 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.

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

2 participants