Skip to content

Commit c73087f

Browse files
committed
Use covariant types in multibind
To avoid the need for explicit typing when using the method.
1 parent 602caec commit c73087f

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

injector/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import types
2424
from abc import ABCMeta, abstractmethod
2525
from dataclasses import dataclass
26+
from collections.abc import Collection, Mapping
2627
from typing import (
2728
TYPE_CHECKING,
2829
Any,
@@ -387,7 +388,7 @@ def multibind(
387388
element_type = get_args(_punch_through_alias(interface))[0]
388389
except IndexError:
389390
raise InvalidInterface(f"Use typing.List[T] or list[T] to specify the element type of the list")
390-
if isinstance(to, list):
391+
if isinstance(to, Collection):
391392
for element in to:
392393
element_binding = self._binder.create_binding(element_type, element, scope)
393394
self.append(element_binding.provider, element_binding.scope)
@@ -415,7 +416,7 @@ def multibind(
415416
raise InvalidInterface(
416417
f"Use typing.Dict[K, V] or dict[K, V] to specify the value type of the dict"
417418
)
418-
if isinstance(to, dict):
419+
if isinstance(to, Mapping):
419420
for key, value in to.items():
420421
element_binding = self._binder.create_binding(value_type, value, scope)
421422
self.append(KeyValueProvider(key, element_binding.provider), element_binding.scope)
@@ -544,7 +545,7 @@ def bind(
544545
def multibind(
545546
self,
546547
interface: Type[List[T]],
547-
to: Union[List[Union[T, Type[T]]], Callable[..., List[T]], Provider[List[T]], Type[T]],
548+
to: Union[Collection[Union[T, Type[T]]], Callable[..., List[T]], Provider[List[T]], Type[T]],
548549
scope: Union[Type['Scope'], 'ScopeDecorator', None] = None,
549550
) -> None: # pragma: no cover
550551
pass
@@ -553,7 +554,7 @@ def multibind(
553554
def multibind(
554555
self,
555556
interface: Type[Dict[K, V]],
556-
to: Union[Dict[K, Union[V, Type[V]]], Callable[..., Dict[K, V]], Provider[Dict[K, V]]],
557+
to: Union[Mapping[K, Union[V, Type[V]]], Callable[..., Dict[K, V]], Provider[Dict[K, V]]],
557558
scope: Union[Type['Scope'], 'ScopeDecorator', None] = None,
558559
) -> None: # pragma: no cover
559560
pass

0 commit comments

Comments
 (0)