Skip to content

Commit b2ce7e5

Browse files
committed
Parent bindings should not affect multibind bindings
This is more in line with how regular bindings work.
1 parent 631375b commit b2ce7e5

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

injector/__init__.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -359,16 +359,7 @@ def append(self, provider: Provider[T], scope: Type['Scope']) -> None:
359359

360360
def get_scoped_providers(self, injector: 'Injector') -> Generator[Provider[T], None, None]:
361361
for binding in self._multi_bindings:
362-
if (
363-
isinstance(binding.provider, ClassProvider)
364-
and binding.scope is NoScope
365-
and self._binder.parent
366-
and self._binder.parent.has_explicit_binding_for(binding.provider._cls)
367-
):
368-
parent_binding, _ = self._binder.parent.get_binding(binding.provider._cls)
369-
scope_binding, _ = self._binder.parent.get_binding(parent_binding.scope)
370-
else:
371-
scope_binding, _ = self._binder.get_binding(binding.scope)
362+
scope_binding, _ = self._binder.get_binding(binding.scope)
372363
scope_instance: Scope = scope_binding.provider.get(injector)
373364
provider_instance = scope_instance.get(binding.interface, binding.provider)
374365
yield provider_instance

injector_test.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -723,19 +723,46 @@ def configure_dict(binder: Binder):
723723
Injector([configure_dict])
724724

725725

726-
def test_multibind_types_respect_the_bound_type_scope() -> None:
726+
def test_multibind_types_are_not_affected_by_the_bound_type_scope() -> None:
727727
def configure(binder: Binder) -> None:
728728
binder.bind(PluginA, to=PluginA, scope=singleton)
729729
binder.multibind(List[Plugin], to=PluginA)
730730

731731
injector = Injector([configure])
732732
first_list = injector.get(List[Plugin])
733733
second_list = injector.get(List[Plugin])
734-
child_injector = injector.create_child_injector()
735-
third_list = child_injector.get(List[Plugin])
736734

737-
assert first_list[0] is second_list[0]
738-
assert third_list[0] is second_list[0]
735+
assert injector.get(PluginA) is injector.get(PluginA)
736+
assert first_list[0] is not injector.get(PluginA)
737+
assert first_list[0] is not second_list[0]
738+
739+
740+
def test_multibind_types_are_not_affected_by_the_bound_type_provider() -> None:
741+
def configure(binder: Binder) -> None:
742+
binder.bind(PluginA, to=InstanceProvider(PluginA()))
743+
binder.multibind(List[Plugin], to=PluginA)
744+
745+
injector = Injector([configure])
746+
first_list = injector.get(List[Plugin])
747+
second_list = injector.get(List[Plugin])
748+
749+
assert injector.get(PluginA) is injector.get(PluginA)
750+
assert first_list[0] is not injector.get(PluginA)
751+
assert first_list[0] is not second_list[0]
752+
753+
754+
def test_multibind_dict_types_use_their_own_bound_providers_and_scopes() -> None:
755+
def configure(binder: Binder) -> None:
756+
binder.bind(PluginA, to=InstanceProvider(PluginA()))
757+
binder.bind(PluginB, to=PluginB, scope=singleton)
758+
binder.multibind(Dict[str, Plugin], to={'a': PluginA, 'b': PluginB})
759+
760+
injector = Injector([configure])
761+
762+
dictionary = injector.get(Dict[str, Plugin])
763+
764+
assert dictionary['a'] is not injector.get(PluginA)
765+
assert dictionary['b'] is not injector.get(PluginB)
739766

740767

741768
def test_multibind_list_scopes_applies_to_the_bound_items() -> None:

0 commit comments

Comments
 (0)