1- from typing import Any , Callable , Dict , Optional , Tuple , Type
1+ from typing import Any , Callable , Dict , Optional , Tuple
2+ from typing_extensions import assert_type
23
34from dependency_injector import providers
45
@@ -7,19 +8,20 @@ class Animal: ...
78
89
910class Cat (Animal ):
10-
1111 @classmethod
1212 def create (cls ) -> Animal :
1313 return cls ()
1414
1515
1616# Test 1: to check the return type (class)
1717provider1 = providers .Callable (Cat )
18- animal1 : Animal = provider1 (1 , 2 , 3 , b = "1" , c = 2 , e = 0.0 )
18+ cat1 = provider1 (1 , 2 , 3 , b = "1" , c = 2 , e = 0.0 )
19+ assert_type (cat1 , Cat )
1920
2021# Test 2: to check the return type (class factory method)
2122provider2 = providers .Callable (Cat .create )
22- animal2 : Animal = provider2 ()
23+ animal2 = provider2 ()
24+ assert_type (animal2 , Animal )
2325
2426# Test 3: to check the .override() method
2527provider3 = providers .Callable (Animal )
@@ -28,24 +30,32 @@ def create(cls) -> Animal:
2830
2931# Test 4: to check the .args & .kwargs attributes
3032provider4 = providers .Callable (Animal )
31- args4 : Tuple [Any ] = provider4 .args
32- kwargs4 : Dict [str , Any ] = provider4 .kwargs
33+ args4 = provider4 .args
34+ kwargs4 = provider4 .kwargs
35+ assert_type (args4 , Tuple [Any ])
36+ assert_type (kwargs4 , Dict [str , Any ])
3337
3438# Test 5: to check the provided instance interface
3539provider5 = providers .Callable (Animal )
36- provided5 : Animal = provider5 .provided ()
37- attr_getter5 : providers .AttributeGetter = provider5 .provided .attr
38- item_getter5 : providers .ItemGetter = provider5 .provided ["item" ]
39- method_caller : providers .MethodCaller = provider5 .provided .method .call (123 , arg = 324 )
40+ provided_val5 = provider5 .provided ()
41+ attr_getter5 = provider5 .provided .attr
42+ item_getter5 = provider5 .provided ["item" ]
43+ method_caller5 = provider5 .provided .method .call (123 , arg = 324 )
44+ assert_type (provided_val5 , Any )
45+ assert_type (attr_getter5 , providers .AttributeGetter )
46+ assert_type (item_getter5 , providers .ItemGetter )
47+ assert_type (method_caller5 , providers .MethodCaller )
4048
4149# Test 6: to check the DelegatedCallable
4250provider6 = providers .DelegatedCallable (Cat )
43- animal6 : Animal = provider6 (1 , 2 , 3 , b = "1" , c = 2 , e = 0.0 )
51+ cat6 = provider6 (1 , 2 , 3 , b = "1" , c = 2 , e = 0.0 )
52+ assert_type (cat6 , Cat )
4453
4554# Test 7: to check the AbstractCallable
4655provider7 = providers .AbstractCallable (Animal )
4756provider7 .override (providers .Callable (Cat ))
48- animal7 : Animal = provider7 (1 , 2 , 3 , b = "1" , c = 2 , e = 0.0 )
57+ animal7 = provider7 (1 , 2 , 3 , b = "1" , c = 2 , e = 0.0 )
58+ assert_type (animal7 , Animal )
4959
5060# Test 8: to check the CallableDelegate __init__
5161provider8 = providers .CallableDelegate (providers .Callable (lambda : None ))
@@ -55,20 +65,22 @@ def create(cls) -> Animal:
5565
5666
5767async def _async9 () -> None :
58- animal1 : Animal = await provider9 (1 , 2 , 3 , b = "1" , c = 2 , e = 0.0 ) # type: ignore
59- animal2 : Animal = await provider9 .async_ (1 , 2 , 3 , b = "1" , c = 2 , e = 0.0 )
68+ await provider9 (1 , 2 , 3 , b = "1" , c = 2 , e = 0.0 ) # type: ignore[misc]
69+ cat9 = await provider9 .async_ (1 , 2 , 3 , b = "1" , c = 2 , e = 0.0 )
70+ assert_type (cat9 , Cat )
6071
6172
6273# Test 10: to check the .provides
6374provider10 = providers .Callable (Cat )
64- provides10 : Optional [ Callable [..., Cat ]] = provider10 .provides
65- assert provides10 is Cat
75+ provides10 = provider10 .provides
76+ assert_type ( provides10 , Optional [ Callable [..., Cat ]])
6677
6778# Test 11: to check the .provides for explicit typevar
6879provider11 = providers .Callable [Animal ](Cat )
69- provides11 : Optional [Callable [..., Animal ]] = provider11 .provides
70- assert provides11 is Cat
80+ provides11 = provider11 .provides
81+ assert_type (provides11 , Optional [Callable [..., Animal ]])
82+
7183
7284# Test 12: to check string imports
73- provider12 : providers . Callable [ Dict [ Any , Any ]] = providers .Callable ("builtins.dict" )
85+ provider12 = providers .Callable ("builtins.dict" )
7486provider12 .set_provides ("builtins.dict" )
0 commit comments