Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions injector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
TYPE_CHECKING,
Any,
Callable,
Collection,
Dict,
Generator,
Generic,
Iterable,
List,
Mapping,
Optional,
Set,
Tuple,
Expand Down Expand Up @@ -387,7 +389,7 @@ def multibind(
element_type = get_args(_punch_through_alias(interface))[0]
except IndexError:
raise InvalidInterface(f"Use typing.List[T] or list[T] to specify the element type of the list")
if isinstance(to, list):
if isinstance(to, Collection):
Copy link

Copilot AI Jan 9, 2026

Choose a reason for hiding this comment

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

Using isinstance(to, Collection) will incorrectly match strings since strings are collections in Python. This could cause the code to iterate over individual characters instead of treating the string as an invalid input. Add an explicit check to exclude strings: if isinstance(to, Collection) and not isinstance(to, (str, bytes)):

Suggested change
if isinstance(to, Collection):
if isinstance(to, Collection) and not isinstance(to, (str, bytes)):

Copilot uses AI. Check for mistakes.
for element in to:
element_binding = self._binder.create_binding(element_type, element, scope)
self.append(element_binding.provider, element_binding.scope)
Expand Down Expand Up @@ -415,7 +417,7 @@ def multibind(
raise InvalidInterface(
f"Use typing.Dict[K, V] or dict[K, V] to specify the value type of the dict"
)
if isinstance(to, dict):
if isinstance(to, Mapping):
for key, value in to.items():
element_binding = self._binder.create_binding(value_type, value, scope)
self.append(KeyValueProvider(key, element_binding.provider), element_binding.scope)
Expand Down Expand Up @@ -544,7 +546,7 @@ def bind(
def multibind(
self,
interface: Type[List[T]],
to: Union[List[Union[T, Type[T]]], Callable[..., List[T]], Provider[List[T]], Type[T]],
to: Union[Collection[Union[T, Type[T]]], Callable[..., List[T]], Provider[List[T]], Type[T]],
scope: Union[Type['Scope'], 'ScopeDecorator', None] = None,
) -> None: # pragma: no cover
pass
Expand All @@ -553,7 +555,7 @@ def multibind(
def multibind(
self,
interface: Type[Dict[K, V]],
to: Union[Dict[K, Union[V, Type[V]]], Callable[..., Dict[K, V]], Provider[Dict[K, V]]],
to: Union[Mapping[K, Union[V, Type[V]]], Callable[..., Dict[K, V]], Provider[Dict[K, V]]],
scope: Union[Type['Scope'], 'ScopeDecorator', None] = None,
) -> None: # pragma: no cover
pass
Expand Down
8 changes: 7 additions & 1 deletion injector_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,19 +733,25 @@ class PluginD(Plugin):
pass


class PluginE(Plugin):
pass


def test_multibind_list_of_plugins():
def configure(binder: Binder):
binder.multibind(List[Plugin], to=PluginA)
binder.multibind(List[Plugin], to=[PluginB, PluginC()])
binder.multibind(List[Plugin], to=lambda: [PluginD()])
binder.multibind(List[Plugin], to=(PluginE,))

injector = Injector([configure])
plugins = injector.get(List[Plugin])
assert len(plugins) == 4
assert len(plugins) == 5
assert isinstance(plugins[0], PluginA)
assert isinstance(plugins[1], PluginB)
assert isinstance(plugins[2], PluginC)
assert isinstance(plugins[3], PluginD)
assert isinstance(plugins[4], PluginE)


def test_multibind_dict_of_plugins():
Expand Down