mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-12-01 14:04:01 +03:00
Add typing stubs for async_() method + tests
This commit is contained in:
parent
227d3494da
commit
644406b594
|
@ -35,7 +35,13 @@ class OverridingContext:
|
|||
|
||||
class Provider(Generic[T]):
|
||||
def __init__(self) -> None: ...
|
||||
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> T: ...
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> Awaitable[T]: ...
|
||||
def async_(self, *args: Injection, **kwargs: Injection) -> Awaitable[T]: ...
|
||||
|
||||
def __deepcopy__(self, memo: Optional[_Dict[Any, Any]]) -> Provider: ...
|
||||
def __str__(self) -> str: ...
|
||||
def __repr__(self) -> str: ...
|
||||
|
@ -62,19 +68,36 @@ class Provider(Generic[T]):
|
|||
|
||||
class Object(Provider, Generic[T]):
|
||||
def __init__(self, provides: T) -> None: ...
|
||||
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> T: ...
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> Awaitable[T]: ...
|
||||
def async_(self, *args: Injection, **kwargs: Injection) -> Awaitable[T]: ...
|
||||
|
||||
|
||||
class Delegate(Provider):
|
||||
def __init__(self, provides: Provider) -> None: ...
|
||||
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> Provider: ...
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> Awaitable[Provider]: ...
|
||||
def async_(self, *args: Injection, **kwargs: Injection) -> Awaitable[Provider]: ...
|
||||
|
||||
@property
|
||||
def provides(self) -> Provider: ...
|
||||
|
||||
|
||||
class Dependency(Provider, Generic[T]):
|
||||
def __init__(self, instance_of: Type[T] = object) -> None: ...
|
||||
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> T: ...
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> Awaitable[T]: ...
|
||||
def async_(self, *args: Injection, **kwargs: Injection) -> Awaitable[T]: ...
|
||||
|
||||
@property
|
||||
def instance_of(self) -> Type[T]: ...
|
||||
def provided_by(self, provider: Provider) -> OverridingContext: ...
|
||||
|
@ -92,7 +115,13 @@ class DependenciesContainer(Object):
|
|||
|
||||
class Callable(Provider, Generic[T]):
|
||||
def __init__(self, provides: _Callable[..., T], *args: Injection, **kwargs: Injection) -> None: ...
|
||||
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> T: ...
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> Awaitable[T]: ...
|
||||
def async_(self, *args: Injection, **kwargs: Injection) -> Awaitable[T]: ...
|
||||
|
||||
@property
|
||||
def provides(self) -> T: ...
|
||||
@property
|
||||
|
@ -135,9 +164,15 @@ class CoroutineDelegate(Delegate):
|
|||
class ConfigurationOption(Provider):
|
||||
UNDEFINED: object
|
||||
def __init__(self, name: Tuple[str], root: Configuration) -> None: ...
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> Any: ...
|
||||
def __getattr__(self, item: str) -> ConfigurationOption: ...
|
||||
def __getitem__(self, item: Union[str, Provider]) -> ConfigurationOption: ...
|
||||
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> Any: ...
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> Awaitable[Any]: ...
|
||||
def async_(self, *args: Injection, **kwargs: Injection) -> Awaitable[Any]: ...
|
||||
|
||||
@property
|
||||
def root(self) -> Configuration: ...
|
||||
def get_name(self) -> str: ...
|
||||
|
@ -162,6 +197,13 @@ class Configuration(Object):
|
|||
def __init__(self, name: str = DEFAULT_NAME, default: Optional[Any] = None) -> None: ...
|
||||
def __getattr__(self, item: str) -> ConfigurationOption: ...
|
||||
def __getitem__(self, item: Union[str, Provider]) -> ConfigurationOption: ...
|
||||
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> Any: ...
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> Awaitable[Any]: ...
|
||||
def async_(self, *args: Injection, **kwargs: Injection) -> Awaitable[Any]: ...
|
||||
|
||||
def get_name(self) -> str: ...
|
||||
def get(self, selector: str) -> Any: ...
|
||||
def set(self, selector: str, value: Any) -> OverridingContext: ...
|
||||
|
@ -176,10 +218,13 @@ class Configuration(Object):
|
|||
class Factory(Provider, Generic[T]):
|
||||
provided_type: Optional[Type]
|
||||
def __init__(self, provides: _Callable[..., T], *args: Injection, **kwargs: Injection) -> None: ...
|
||||
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> T: ...
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> Awaitable[T]: ...
|
||||
def async_(self, *args: Injection, **kwargs: Injection) -> Awaitable[T]: ...
|
||||
|
||||
@property
|
||||
def cls(self) -> T: ...
|
||||
@property
|
||||
|
@ -214,8 +259,14 @@ class FactoryDelegate(Delegate):
|
|||
|
||||
class FactoryAggregate(Provider):
|
||||
def __init__(self, **factories: Factory): ...
|
||||
def __call__(self, factory_name: str, *args: Injection, **kwargs: Injection) -> Any: ...
|
||||
def __getattr__(self, factory_name: str) -> Factory: ...
|
||||
|
||||
@overload
|
||||
def __call__(self, factory_name: str, *args: Injection, **kwargs: Injection) -> Any: ...
|
||||
@overload
|
||||
def __call__(self, factory_name: str, *args: Injection, **kwargs: Injection) -> Awaitable[Any]: ...
|
||||
def async_(self, factory_name: str, *args: Injection, **kwargs: Injection) -> Awaitable[Any]: ...
|
||||
|
||||
@property
|
||||
def factories(self) -> _Dict[str, Factory]: ...
|
||||
|
||||
|
@ -223,7 +274,13 @@ class FactoryAggregate(Provider):
|
|||
class BaseSingleton(Provider, Generic[T]):
|
||||
provided_type = Optional[Type]
|
||||
def __init__(self, provides: _Callable[..., T], *args: Injection, **kwargs: Injection) -> None: ...
|
||||
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> T: ...
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> Awaitable[T]: ...
|
||||
def async_(self, *args: Injection, **kwargs: Injection) -> Awaitable[T]: ...
|
||||
|
||||
@property
|
||||
def cls(self) -> T: ...
|
||||
@property
|
||||
|
@ -244,25 +301,25 @@ class BaseSingleton(Provider, Generic[T]):
|
|||
def reset(self) -> None: ...
|
||||
|
||||
|
||||
class Singleton(BaseSingleton): ...
|
||||
class Singleton(BaseSingleton[T]): ...
|
||||
|
||||
|
||||
class DelegatedSingleton(Singleton): ...
|
||||
class DelegatedSingleton(Singleton[T]): ...
|
||||
|
||||
|
||||
class ThreadSafeSingleton(Singleton): ...
|
||||
class ThreadSafeSingleton(Singleton[T]): ...
|
||||
|
||||
|
||||
class DelegatedThreadSafeSingleton(ThreadSafeSingleton): ...
|
||||
class DelegatedThreadSafeSingleton(ThreadSafeSingleton[T]): ...
|
||||
|
||||
|
||||
class ThreadLocalSingleton(BaseSingleton): ...
|
||||
class ThreadLocalSingleton(BaseSingleton[T]): ...
|
||||
|
||||
|
||||
class DelegatedThreadLocalSingleton(ThreadLocalSingleton): ...
|
||||
class DelegatedThreadLocalSingleton(ThreadLocalSingleton[T]): ...
|
||||
|
||||
|
||||
class AbstractSingleton(BaseSingleton):
|
||||
class AbstractSingleton(BaseSingleton[T]):
|
||||
def override(self, provider: BaseSingleton) -> OverridingContext: ...
|
||||
|
||||
|
||||
|
@ -272,7 +329,13 @@ class SingletonDelegate(Delegate):
|
|||
|
||||
class List(Provider):
|
||||
def __init__(self, *args: Injection): ...
|
||||
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> _List[Any]: ...
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> Awaitable[_List[Any]]: ...
|
||||
def async_(self, *args: Injection, **kwargs: Injection) -> Awaitable[_List[Any]]: ...
|
||||
|
||||
@property
|
||||
def args(self) -> Tuple[Injection]: ...
|
||||
def add_args(self, *args: Injection) -> List: ...
|
||||
|
@ -282,7 +345,13 @@ class List(Provider):
|
|||
|
||||
class Dict(Provider):
|
||||
def __init__(self, dict_: Optional[_Dict[Any, Injection]] = None, **kwargs: Injection): ...
|
||||
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> _Dict[Any, Any]: ...
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> Awaitable[_Dict[Any, Any]]: ...
|
||||
def async_(self, *args: Injection, **kwargs: Injection) -> Awaitable[_Dict[Any, Any]]: ...
|
||||
|
||||
@property
|
||||
def kwargs(self) -> _Dict[Any, Injection]: ...
|
||||
def add_kwargs(self, dict_: Optional[_Dict[Any, Injection]] = None, **kwargs: Injection) -> Dict: ...
|
||||
|
@ -303,11 +372,13 @@ class Resource(Provider, Generic[T]):
|
|||
def __init__(self, initializer: _Callable[..., _Coroutine[Injection, Injection, T]], *args: Injection, **kwargs: Injection) -> None: ...
|
||||
@overload
|
||||
def __init__(self, initializer: _Callable[..., T], *args: Injection, **kwargs: Injection) -> None: ...
|
||||
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> T: ...
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> Awaitable[T]: ...
|
||||
def async_(self, *args: Injection, **kwargs: Injection) -> Awaitable[T]: ...
|
||||
|
||||
@property
|
||||
def args(self) -> Tuple[Injection]: ...
|
||||
def add_args(self, *args: Injection) -> Resource: ...
|
||||
|
@ -327,16 +398,28 @@ class Resource(Provider, Generic[T]):
|
|||
class Container(Provider):
|
||||
|
||||
def __init__(self, container_cls: Type[T], container: Optional[T] = None, **overriding_providers: Provider) -> None: ...
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> T: ...
|
||||
def __getattr__(self, name: str) -> Provider: ...
|
||||
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> T: ...
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> Awaitable[T]: ...
|
||||
def async_(self, *args: Injection, **kwargs: Injection) -> Awaitable[T]: ...
|
||||
|
||||
@property
|
||||
def container(self) -> T: ...
|
||||
|
||||
|
||||
class Selector(Provider):
|
||||
def __init__(self, selector: _Callable[..., Any], **providers: Provider): ...
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> Any: ...
|
||||
def __getattr__(self, name: str) -> Provider: ...
|
||||
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> Any: ...
|
||||
@overload
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> Awaitable[Any]: ...
|
||||
def async_(self, *args: Injection, **kwargs: Injection) -> Awaitable[Any]: ...
|
||||
|
||||
@property
|
||||
def providers(self) -> _Dict[str, Provider]: ...
|
||||
|
||||
|
|
|
@ -50,3 +50,9 @@ animal7: Animal = provider7(1, 2, 3, b='1', c=2, e=0.0)
|
|||
|
||||
# Test 8: to check the CallableDelegate __init__
|
||||
provider8 = providers.CallableDelegate(providers.Callable(lambda: None))
|
||||
|
||||
# Test 9: to check the return type with await
|
||||
provider9 = providers.Callable(Cat)
|
||||
async def _async9() -> None:
|
||||
animal1: Animal = await provider9(1, 2, 3, b='1', c=2, e=0.0) # type: ignore
|
||||
animal2: Animal = await provider9.async_(1, 2, 3, b='1', c=2, e=0.0)
|
||||
|
|
|
@ -4,3 +4,9 @@ from dependency_injector import providers
|
|||
# Test 1: to check the return type
|
||||
provider1 = providers.Delegate(providers.Provider())
|
||||
var1: providers.Provider = provider1()
|
||||
|
||||
# Test 2: to check the return type with await
|
||||
provider2 = providers.Delegate(providers.Provider())
|
||||
async def _async2() -> None:
|
||||
var1: providers.Provider = await provider2() # type: ignore
|
||||
var2: providers.Provider = await provider2.async_()
|
||||
|
|
|
@ -20,3 +20,9 @@ var1: Animal = provider1()
|
|||
# Test 2: to check the return type
|
||||
provider2 = providers.Dependency(instance_of=Animal)
|
||||
var2: Type[Animal] = provider2.instance_of
|
||||
|
||||
# Test 3: to check the return type with await
|
||||
provider3 = providers.Dependency(instance_of=Animal)
|
||||
async def _async3() -> None:
|
||||
var1: Animal = await provider3() # type: ignore
|
||||
var2: Animal = await provider3.async_()
|
||||
|
|
|
@ -35,3 +35,13 @@ provider5 = providers.Dict(
|
|||
a2=providers.Factory(object),
|
||||
)
|
||||
provided5: providers.ProvidedInstance = provider5.provided
|
||||
|
||||
|
||||
# Test 6: to check the return type with await
|
||||
provider6 = providers.Dict(
|
||||
a1=providers.Factory(object),
|
||||
a2=providers.Factory(object),
|
||||
)
|
||||
async def _async3() -> None:
|
||||
var1: Dict[Any, Any] = await provider6() # type: ignore
|
||||
var2: Dict[Any, Any] = await provider6.async_()
|
||||
|
|
|
@ -66,3 +66,9 @@ val9: Any = provider9('a')
|
|||
# Test 10: to check the explicit typing
|
||||
factory10: providers.Provider[Animal] = providers.Factory(Cat)
|
||||
animal10: Animal = factory10()
|
||||
|
||||
# Test 11: to check the return type with await
|
||||
provider11 = providers.Factory(Cat)
|
||||
async def _async11() -> None:
|
||||
animal1: Animal = await provider11(1, 2, 3, b='1', c=2, e=0.0) # type: ignore
|
||||
animal2: Animal = await provider11.async_(1, 2, 3, b='1', c=2, e=0.0)
|
||||
|
|
|
@ -27,3 +27,12 @@ provided3: providers.ProvidedInstance = provider3.provided
|
|||
attr_getter3: providers.AttributeGetter = provider3.provided.attr
|
||||
item_getter3: providers.ItemGetter = provider3.provided['item']
|
||||
method_caller3: providers.MethodCaller = provider3.provided.method.call(123, arg=324)
|
||||
|
||||
# Test 4: to check the return type with await
|
||||
provider4 = providers.List(
|
||||
providers.Factory(object),
|
||||
providers.Factory(object),
|
||||
)
|
||||
async def _async4() -> None:
|
||||
var1: List[Any] = await provider4() # type: ignore
|
||||
var2: List[Any] = await provider4.async_()
|
||||
|
|
|
@ -11,3 +11,9 @@ provided2: providers.ProvidedInstance = provider2.provided
|
|||
attr_getter2: providers.AttributeGetter = provider2.provided.attr
|
||||
item_getter2: providers.ItemGetter = provider2.provided['item']
|
||||
method_caller2: providers.MethodCaller = provider2.provided.method.call(123, arg=324)
|
||||
|
||||
# Test 3: to check the return type with await
|
||||
provider3 = providers.Object(int(3))
|
||||
async def _async3() -> None:
|
||||
var1: int = await provider3() # type: ignore
|
||||
var2: int = await provider3.async_()
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
from typing import Any
|
||||
|
||||
from dependency_injector import providers
|
||||
|
||||
|
||||
|
@ -7,7 +9,7 @@ provider1 = providers.Selector(
|
|||
a=providers.Factory(object),
|
||||
b=providers.Factory(object),
|
||||
)
|
||||
var1: int = provider1()
|
||||
var1: Any = provider1()
|
||||
|
||||
# Test 2: to check the provided instance interface
|
||||
provider2 = providers.Selector(
|
||||
|
@ -27,3 +29,13 @@ provider3 = providers.Selector(
|
|||
b=providers.Factory(object),
|
||||
)
|
||||
attr3: providers.Provider = provider3.a
|
||||
|
||||
# Test 4: to check the return type with await
|
||||
provider4 = providers.Selector(
|
||||
lambda: 'a',
|
||||
a=providers.Factory(object),
|
||||
b=providers.Factory(object),
|
||||
)
|
||||
async def _async4() -> None:
|
||||
var1: Any = await provider4() # type: ignore
|
||||
var2: Any = await provider4.async_()
|
||||
|
|
|
@ -69,3 +69,9 @@ animal11: Animal = provider11(1, 2, 3, b='1', c=2, e=0.0)
|
|||
|
||||
# Test 12: to check the SingletonDelegate __init__
|
||||
provider12 = providers.SingletonDelegate(providers.Singleton(object))
|
||||
|
||||
# Test 13: to check the return type with await
|
||||
provider13 = providers.Singleton(Cat)
|
||||
async def _async13() -> None:
|
||||
animal1: Animal = await provider13(1, 2, 3, b='1', c=2, e=0.0) # type: ignore
|
||||
animal2: Animal = await provider13.async_(1, 2, 3, b='1', c=2, e=0.0)
|
||||
|
|
Loading…
Reference in New Issue
Block a user