Skip to content

Commit bd8f001

Browse files
committed
test: Separation of more types with multibind
1 parent b4fd3f4 commit bd8f001

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

injector_test.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,9 @@ def provide_description(self, age: int, weight: float) -> str:
636636
Names = NewType('Names', List[str])
637637
Passwords = NewType('Passwords', Dict[str, str])
638638

639+
Animals = Annotated[List[str], 'Animals']
640+
AnimalFoods = Annotated[Dict[str, str], 'AnimalFoods']
641+
639642

640643
def test_multibind():
641644
# First let's have some explicit multibindings
@@ -648,6 +651,12 @@ def configure(binder):
648651
# To see that NewTypes are treated distinctly
649652
binder.multibind(Names, to=['Bob'])
650653
binder.multibind(Passwords, to={'Bob': 'password1'})
654+
# To see that Annotated collections are treated distinctly
655+
binder.multibind(Animals, to=['Dog'])
656+
binder.multibind(AnimalFoods, to={'Dog': 'meat'})
657+
# To see that collections of Annotated types are treated distinctly
658+
binder.multibind(List[City], to=[City('Stockholm')])
659+
binder.multibind(Dict[str, City], to={'Sweden': City('Stockholm')})
651660

652661
# Then @multiprovider-decorated Module methods
653662
class CustomModule(Module):
@@ -669,11 +678,27 @@ def provide_str_to_int_mapping(self) -> Dict[str, int]:
669678

670679
@multiprovider
671680
def provide_names(self) -> Names:
672-
return ['Alice', 'Clarice']
681+
return Names(['Alice', 'Clarice'])
673682

674683
@multiprovider
675684
def provide_passwords(self) -> Passwords:
676-
return {'Alice': 'aojrioeg3', 'Clarice': 'clarice30'}
685+
return Passwords({'Alice': 'aojrioeg3', 'Clarice': 'clarice30'})
686+
687+
@multiprovider
688+
def provide_animals(self) -> Animals:
689+
return ['Cat', 'Fish']
690+
691+
@multiprovider
692+
def provide_animal_foods(self) -> AnimalFoods:
693+
return {'Cat': 'milk', 'Fish': 'flakes'}
694+
695+
@multiprovider
696+
def provide_cities(self) -> List[City]:
697+
return [City('New York'), City('Tokyo')]
698+
699+
@multiprovider
700+
def provide_city_mapping(self) -> Dict[str, City]:
701+
return {'USA': City('New York'), 'Japan': City('Tokyo')}
677702

678703
injector = Injector([configure, CustomModule])
679704
assert injector.get(List[str]) == ['not a name', 'not a name either']
@@ -682,6 +707,10 @@ def provide_passwords(self) -> Passwords:
682707
assert injector.get(Dict[str, int]) == {'weight': 12, 'height': 33}
683708
assert injector.get(Names) == ['Bob', 'Alice', 'Clarice']
684709
assert injector.get(Passwords) == {'Bob': 'password1', 'Alice': 'aojrioeg3', 'Clarice': 'clarice30'}
710+
assert injector.get(Animals) == ['Dog', 'Cat', 'Fish']
711+
assert injector.get(AnimalFoods) == {'Dog': 'meat', 'Cat': 'milk', 'Fish': 'flakes'}
712+
assert injector.get(List[City]) == ['Stockholm', 'New York', 'Tokyo']
713+
assert injector.get(Dict[str, City]) == {'Sweden': 'Stockholm', 'USA': 'New York', 'Japan': 'Tokyo'}
685714

686715

687716
class Plugin(abc.ABC):

0 commit comments

Comments
 (0)