Skip to content

Commit 143b86d

Browse files
committed
Fix callable.py
1 parent 1cc94e9 commit 143b86d

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

tests/typing/callable.py

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
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

34
from dependency_injector import providers
45

@@ -7,19 +8,20 @@ class Animal: ...
78

89

910
class Cat(Animal):
10-
1111
@classmethod
1212
def create(cls) -> Animal:
1313
return cls()
1414

1515

1616
# Test 1: to check the return type (class)
1717
provider1 = 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)
2122
provider2 = 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
2527
provider3 = providers.Callable(Animal)
@@ -28,24 +30,34 @@ def create(cls) -> Animal:
2830

2931
# Test 4: to check the .args & .kwargs attributes
3032
provider4 = 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+
# TODO: Change Callable.kwargs to Dict[str, Any]? Then adjust test back to Dict[str, Any]
37+
assert_type(kwargs4, Dict[Any, Any])
3338

3439
# Test 5: to check the provided instance interface
3540
provider5 = 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)
41+
provided_val5 = provider5.provided()
42+
attr_getter5 = provider5.provided.attr
43+
item_getter5 = provider5.provided["item"]
44+
method_caller5 = provider5.provided.method.call(123, arg=324)
45+
# TODO: Remove explicit typing of Provider.provided return type
46+
assert_type(provided_val5, Any)
47+
assert_type(attr_getter5, providers.AttributeGetter)
48+
assert_type(item_getter5, providers.ItemGetter)
49+
assert_type(method_caller5, providers.MethodCaller)
4050

4151
# Test 6: to check the DelegatedCallable
4252
provider6 = providers.DelegatedCallable(Cat)
43-
animal6: Animal = provider6(1, 2, 3, b="1", c=2, e=0.0)
53+
cat6 = provider6(1, 2, 3, b="1", c=2, e=0.0)
54+
assert_type(cat6, Cat)
4455

4556
# Test 7: to check the AbstractCallable
4657
provider7 = providers.AbstractCallable(Animal)
4758
provider7.override(providers.Callable(Cat))
48-
animal7: Animal = provider7(1, 2, 3, b="1", c=2, e=0.0)
59+
animal7 = provider7(1, 2, 3, b="1", c=2, e=0.0)
60+
assert_type(animal7, Animal)
4961

5062
# Test 8: to check the CallableDelegate __init__
5163
provider8 = providers.CallableDelegate(providers.Callable(lambda: None))
@@ -55,20 +67,23 @@ def create(cls) -> Animal:
5567

5668

5769
async 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)
70+
await provider9(1, 2, 3, b="1", c=2, e=0.0) # type: ignore[misc]
71+
cat9 = await provider9.async_(1, 2, 3, b="1", c=2, e=0.0)
72+
assert_type(cat9, Cat)
6073

6174

6275
# Test 10: to check the .provides
6376
provider10 = providers.Callable(Cat)
64-
provides10: Optional[Callable[..., Cat]] = provider10.provides
65-
assert provides10 is Cat
77+
provides10 = provider10.provides
78+
assert_type(provides10, Optional[Callable[..., Cat]])
6679

6780
# Test 11: to check the .provides for explicit typevar
6881
provider11 = providers.Callable[Animal](Cat)
69-
provides11: Optional[Callable[..., Animal]] = provider11.provides
70-
assert provides11 is Cat
82+
provides11 = provider11.provides
83+
assert_type(provides11, Optional[Callable[..., Animal]])
84+
7185

7286
# Test 12: to check string imports
73-
provider12: providers.Callable[Dict[Any, Any]] = providers.Callable("builtins.dict")
87+
# TODO: Use T_Any as Callable typevar? Then remove type-ignore
88+
provider12 = providers.Callable("builtins.dict") # type: ignore[var-annotated]
7489
provider12.set_provides("builtins.dict")

0 commit comments

Comments
 (0)