26
26
Binder ,
27
27
CallError ,
28
28
Injector ,
29
+ MultiBindClassProvider ,
29
30
Scope ,
30
31
InstanceProvider ,
31
32
ClassProvider ,
@@ -473,6 +474,14 @@ def provide_description(self, age: int, weight: float) -> str:
473
474
474
475
475
476
def test_multibind ():
477
+ class A :
478
+ def print (self ) -> str :
479
+ return 'A'
480
+
481
+ class B (A ):
482
+ def print (self ) -> str :
483
+ return 'B'
484
+
476
485
# First let's have some explicit multibindings
477
486
def configure (binder ):
478
487
binder .multibind (List [str ], to = ['not a name' ])
@@ -483,6 +492,8 @@ def configure(binder):
483
492
# To see that NewTypes are treated distinctly
484
493
binder .multibind (Names , to = ['Bob' ])
485
494
binder .multibind (Passwords , to = {'Bob' : 'password1' })
495
+ # To see that MultiBindClassProvider works for lists of types
496
+ binder .multibind (List [A ], to = MultiBindClassProvider ([A , B ]))
486
497
487
498
# Then @multiprovider-decorated Module methods
488
499
class CustomModule (Module ):
@@ -517,6 +528,8 @@ def provide_passwords(self) -> Passwords:
517
528
assert injector .get (Dict [str , int ]) == {'weight' : 12 , 'height' : 33 }
518
529
assert injector .get (Names ) == ['Bob' , 'Alice' , 'Clarice' ]
519
530
assert injector .get (Passwords ) == {'Bob' : 'password1' , 'Alice' : 'aojrioeg3' , 'Clarice' : 'clarice30' }
531
+ assert injector .get (List [A ])[0 ].print () == 'A'
532
+ assert injector .get (List [A ])[1 ].print () == 'B'
520
533
521
534
522
535
def test_regular_bind_and_provider_dont_work_with_multibind ():
0 commit comments