Fix type annotations for .provides

This commit is contained in:
Thiago Hiromi 2021-08-18 18:18:04 +02:00
parent 48df949cd5
commit b66f9dc3f9
6 changed files with 40 additions and 9 deletions

View File

@ -84,6 +84,7 @@ class Provider(Generic[T]):
class Object(Provider[T]):
def __init__(self, provides: Optional[T] = None) -> None: ...
@property
def provides(self) -> Optional[T]: ...
def set_provides(self, provides: Optional[T]) -> Object: ...
@ -144,7 +145,7 @@ class DependenciesContainer(Object):
class Callable(Provider[T]):
def __init__(self, provides: Optional[_Callable[..., T]] = None, *args: Injection, **kwargs: Injection) -> None: ...
@property
def provides(self) -> Optional[T]: ...
def provides(self) -> Type[T]: ...
def set_provides(self, provides: Optional[_Callable[..., T]]) -> Callable[T]: ...
@property
def args(self) -> Tuple[Injection]: ...
@ -249,9 +250,9 @@ class Factory(Provider[T]):
provided_type: Optional[Type]
def __init__(self, provides: Optional[_Callable[..., T]] = None, *args: Injection, **kwargs: Injection) -> None: ...
@property
def cls(self) -> T: ...
def cls(self) -> Type[T]: ...
@property
def provides(self) -> T: ...
def provides(self) -> Type[T]: ...
def set_provides(self, provides: Optional[_Callable[..., T]]) -> Factory[T]: ...
@property
def args(self) -> Tuple[Injection]: ...
@ -300,9 +301,9 @@ class BaseSingleton(Provider[T]):
provided_type = Optional[Type]
def __init__(self, provides: Optional[_Callable[..., T]] = None, *args: Injection, **kwargs: Injection) -> None: ...
@property
def cls(self) -> T: ...
def cls(self) -> Type[T]: ...
@property
def provides(self) -> T: ...
def provides(self) -> Type[T]: ...
def set_provides(self, provides: Optional[_Callable[..., T]]) -> BaseSingleton[T]: ...
@property
def args(self) -> Tuple[Injection]: ...

View File

@ -1,4 +1,4 @@
from typing import Tuple, Any, Dict
from typing import Tuple, Any, Dict, Type
from dependency_injector import providers
@ -56,3 +56,8 @@ 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)
# Test 10: to check the provides attr
provider10 = providers.Callable(Cat)
provided_cls: Type[Cat] = provider10.provides
assert provided_cls is Cat

View File

@ -1,5 +1,6 @@
from dependency_injector import providers
from typing import Optional
from dependency_injector import providers
# Test 1: to check the return type
provider1 = providers.Delegate(providers.Provider())
@ -10,3 +11,7 @@ provider2 = providers.Delegate(providers.Provider())
async def _async2() -> None:
var1: providers.Provider = await provider2() # type: ignore
var2: providers.Provider = await provider2.async_()
# Test 3: to check class type from provider
provider3 = providers.Delegate(providers.Provider())
provided_provides: Optional[providers.Provider] = provider3.provides

View File

@ -1,4 +1,4 @@
from typing import Tuple, Any, Dict
from typing import Tuple, Any, Dict, Type
from dependency_injector import providers
@ -72,3 +72,10 @@ 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)
# Test 12: to check class type from provider
factory_provider = providers.Factory(int, '1')
factory_provided_cls: Type[int] = factory_provider.cls
assert factory_provided_cls("1") == 1
factory_provided_provides: Type[int] = factory_provider.provides
assert factory_provided_provides("1") == 1

View File

@ -1,3 +1,5 @@
from typing import Type, Optional
from dependency_injector import providers
@ -17,3 +19,7 @@ provider3 = providers.Object(int(3))
async def _async3() -> None:
var1: int = await provider3() # type: ignore
var2: int = await provider3.async_()
# Test 4: to check class type from provider
provider4 = providers.Object(int('1'))
provided_provides: Optional[int] = provider4.provides

View File

@ -1,4 +1,4 @@
from typing import Tuple, Any, Dict
from typing import Tuple, Any, Dict, Type
from dependency_injector import providers
@ -75,3 +75,10 @@ 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)
# Test 14: to check class from provider
provider14 = providers.Singleton(Cat)
provided_cls: Type[Cat] = provider14.cls
assert issubclass(provided_cls, Cat)
provided_provides: Type[Cat] = provider14.provides
assert issubclass(provided_provides, Cat)