Fix callable.py

This commit is contained in:
Leonardus Chen 2025-10-25 14:12:26 +07:00
parent 9111f5d4f6
commit 35ecd2cb97

View File

@ -1,4 +1,5 @@
from typing import Any, Callable, Dict, Optional, Tuple, Type from typing import Any, Callable, Dict, Optional, Tuple
from typing_extensions import assert_type
from dependency_injector import providers from dependency_injector import providers
@ -7,7 +8,6 @@ class Animal: ...
class Cat(Animal): class Cat(Animal):
@classmethod @classmethod
def create(cls) -> Animal: def create(cls) -> Animal:
return cls() return cls()
@ -15,11 +15,13 @@ class Cat(Animal):
# Test 1: to check the return type (class) # Test 1: to check the return type (class)
provider1 = providers.Callable(Cat) provider1 = providers.Callable(Cat)
animal1: Animal = provider1(1, 2, 3, b="1", c=2, e=0.0) cat1 = provider1(1, 2, 3, b="1", c=2, e=0.0)
assert_type(cat1, Cat)
# Test 2: to check the return type (class factory method) # Test 2: to check the return type (class factory method)
provider2 = providers.Callable(Cat.create) provider2 = providers.Callable(Cat.create)
animal2: Animal = provider2() animal2 = provider2()
assert_type(animal2, Animal)
# Test 3: to check the .override() method # Test 3: to check the .override() method
provider3 = providers.Callable(Animal) provider3 = providers.Callable(Animal)
@ -28,24 +30,34 @@ with provider3.override(providers.Callable(Cat)):
# Test 4: to check the .args & .kwargs attributes # Test 4: to check the .args & .kwargs attributes
provider4 = providers.Callable(Animal) provider4 = providers.Callable(Animal)
args4: Tuple[Any] = provider4.args args4 = provider4.args
kwargs4: Dict[str, Any] = provider4.kwargs kwargs4 = provider4.kwargs
assert_type(args4, Tuple[Any])
# TODO: Change Callable.kwargs to Dict[str, Any]? Then adjust test back to Dict[str, Any]
assert_type(kwargs4, Dict[Any, Any])
# Test 5: to check the provided instance interface # Test 5: to check the provided instance interface
provider5 = providers.Callable(Animal) provider5 = providers.Callable(Animal)
provided5: Animal = provider5.provided() provided_val5 = provider5.provided()
attr_getter5: providers.AttributeGetter = provider5.provided.attr attr_getter5 = provider5.provided.attr
item_getter5: providers.ItemGetter = provider5.provided["item"] item_getter5 = provider5.provided["item"]
method_caller: providers.MethodCaller = provider5.provided.method.call(123, arg=324) method_caller5 = provider5.provided.method.call(123, arg=324)
# TODO: Remove explicit typing of Provider.provided return type
assert_type(provided_val5, Any)
assert_type(attr_getter5, providers.AttributeGetter)
assert_type(item_getter5, providers.ItemGetter)
assert_type(method_caller5, providers.MethodCaller)
# Test 6: to check the DelegatedCallable # Test 6: to check the DelegatedCallable
provider6 = providers.DelegatedCallable(Cat) provider6 = providers.DelegatedCallable(Cat)
animal6: Animal = provider6(1, 2, 3, b="1", c=2, e=0.0) cat6 = provider6(1, 2, 3, b="1", c=2, e=0.0)
assert_type(cat6, Cat)
# Test 7: to check the AbstractCallable # Test 7: to check the AbstractCallable
provider7 = providers.AbstractCallable(Animal) provider7 = providers.AbstractCallable(Animal)
provider7.override(providers.Callable(Cat)) provider7.override(providers.Callable(Cat))
animal7: Animal = provider7(1, 2, 3, b="1", c=2, e=0.0) animal7 = provider7(1, 2, 3, b="1", c=2, e=0.0)
assert_type(animal7, Animal)
# Test 8: to check the CallableDelegate __init__ # Test 8: to check the CallableDelegate __init__
provider8 = providers.CallableDelegate(providers.Callable(lambda: None)) provider8 = providers.CallableDelegate(providers.Callable(lambda: None))
@ -55,20 +67,23 @@ provider9 = providers.Callable(Cat)
async def _async9() -> None: async def _async9() -> None:
animal1: Animal = await provider9(1, 2, 3, b="1", c=2, e=0.0) # type: ignore await provider9(1, 2, 3, b="1", c=2, e=0.0) # type: ignore[misc]
animal2: Animal = await provider9.async_(1, 2, 3, b="1", c=2, e=0.0) cat9 = await provider9.async_(1, 2, 3, b="1", c=2, e=0.0)
assert_type(cat9, Cat)
# Test 10: to check the .provides # Test 10: to check the .provides
provider10 = providers.Callable(Cat) provider10 = providers.Callable(Cat)
provides10: Optional[Callable[..., Cat]] = provider10.provides provides10 = provider10.provides
assert provides10 is Cat assert_type(provides10, Optional[Callable[..., Cat]])
# Test 11: to check the .provides for explicit typevar # Test 11: to check the .provides for explicit typevar
provider11 = providers.Callable[Animal](Cat) provider11 = providers.Callable[Animal](Cat)
provides11: Optional[Callable[..., Animal]] = provider11.provides provides11 = provider11.provides
assert provides11 is Cat assert_type(provides11, Optional[Callable[..., Animal]])
# Test 12: to check string imports # Test 12: to check string imports
provider12: providers.Callable[Dict[Any, Any]] = providers.Callable("builtins.dict") # TODO: Use T_Any as Callable typevar? Then remove type-ignore
provider12 = providers.Callable("builtins.dict") # type: ignore[var-annotated]
provider12.set_provides("builtins.dict") provider12.set_provides("builtins.dict")