mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-07-08 22:33:15 +03:00
Add stub for Callable providers
This commit is contained in:
parent
e8fc059602
commit
fd7d39de5b
|
@ -1,6 +1,6 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from typing import TypeVar, Generic, Type, Callable, Any, Tuple, Optional, Dict, Union
|
||||
from typing import TypeVar, Generic, Type, Callable as _Callable, Any, Tuple, Optional, Dict, Union
|
||||
|
||||
Injection = Any
|
||||
T = TypeVar('T')
|
||||
|
@ -30,9 +30,35 @@ class Provider:
|
|||
def provider(self) -> Provider: ...
|
||||
|
||||
|
||||
class Callable(Provider, Generic[T]):
|
||||
def __init__(self, provides: _Callable[..., T], *args: Injection, **kwargs: Injection) -> None: ...
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> T: ...
|
||||
@property
|
||||
def provides(self) -> T: ...
|
||||
@property
|
||||
def provided(self) -> ProvidedInstance: ...
|
||||
@property
|
||||
def args(self) -> Tuple[Injection]: ...
|
||||
def add_args(self, *args: Injection) -> Callable[T]: ...
|
||||
def set_args(self, *args: Injection) -> Callable[T]: ...
|
||||
def clear_args(self) -> Callable[T]: ...
|
||||
@property
|
||||
def kwargs(self) -> Dict[str, Injection]: ...
|
||||
def add_kwargs(self, **kwargs: Injection) -> Callable[T]: ...
|
||||
def set_kwargs(self, **kwargs: Injection) -> Callable[T]: ...
|
||||
def clear_kwargs(self) -> Callable[T]: ...
|
||||
|
||||
|
||||
class DelegatedCallable(Callable): ...
|
||||
|
||||
|
||||
class AbstractCallable(Callable):
|
||||
def override(self, provider: Callable) -> OverridingContext: ...
|
||||
|
||||
|
||||
class Factory(Provider, Generic[T]):
|
||||
provided_type: Optional[Type]
|
||||
def __init__(self, provides: Callable[..., T], *args: Injection, **kwargs: Injection) -> None: ...
|
||||
def __init__(self, provides: _Callable[..., T], *args: Injection, **kwargs: Injection) -> None: ...
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> T: ...
|
||||
@property
|
||||
def cls(self) -> T: ...
|
||||
|
|
49
tests/typing/callable.py
Normal file
49
tests/typing/callable.py
Normal file
|
@ -0,0 +1,49 @@
|
|||
from typing import Tuple, Any, Dict
|
||||
|
||||
from dependency_injector import providers
|
||||
|
||||
|
||||
class Animal:
|
||||
...
|
||||
|
||||
|
||||
class Cat(Animal):
|
||||
|
||||
@classmethod
|
||||
def create(cls) -> Animal:
|
||||
return cls()
|
||||
|
||||
|
||||
# Test 1: to check the return type (class)
|
||||
provider1 = providers.Callable(Cat)
|
||||
animal1: Animal = provider1(1, 2, 3, b='1', c=2, e=0.0)
|
||||
|
||||
# Test 2: to check the return type (class factory method)
|
||||
provider2 = providers.Callable(Cat.create)
|
||||
animal2: Animal = provider2()
|
||||
|
||||
# Test 3: to check the .override() method
|
||||
provider3 = providers.Callable(Animal)
|
||||
with provider3.override(providers.Callable(Cat)):
|
||||
provider3()
|
||||
|
||||
# Test 4: to check the .args & .kwargs attributes
|
||||
provider4 = providers.Callable(Animal)
|
||||
args4: Tuple[Any] = provider4.args
|
||||
kwargs4: Dict[str, Any] = provider4.kwargs
|
||||
|
||||
# Test 5: to check the provided instance interface
|
||||
provider5 = providers.Callable(Animal)
|
||||
provided5: providers.ProvidedInstance = provider5.provided
|
||||
attr_getter5: providers.AttributeGetter = provider5.provided.attr
|
||||
item_getter5: providers.ItemGetter = provider5.provided['item']
|
||||
method_caller: providers.MethodCaller = provider5.provided.method.call(123, arg=324)
|
||||
|
||||
# Test 6: to check the DelegatedCallable
|
||||
provider6 = providers.DelegatedCallable(Cat)
|
||||
animal6: Animal = provider6(1, 2, 3, b='1', c=2, e=0.0)
|
||||
|
||||
# Test 7: to check the AbstractCallable
|
||||
provider7 = providers.AbstractCallable(Animal)
|
||||
provider7.override(providers.Callable(Cat))
|
||||
animal7: Animal = provider7(1, 2, 3, b='1', c=2, e=0.0)
|
Loading…
Reference in New Issue
Block a user