Add typing tests

This commit is contained in:
Roman Mogylatov 2020-10-23 21:34:44 -04:00
parent 708bf27bce
commit a3f04bd34e
3 changed files with 52 additions and 11 deletions

View File

@ -13,7 +13,9 @@ from typing import (
Optional,
Union,
Coroutine as _Coroutine,
Iterator as _Iterator,
Generator as _Generator,
overload,
)
from . import resources
@ -278,16 +280,12 @@ class Dict(Provider):
class Resource(Provider, Generic[T]):
def __init__(
self,
initializer: Union[
resources.Resource,
_Generator[T, ..., ...],
_Callable[..., T],
],
*args: Injection,
**kwargs: Injection,
): ...
@overload
def __init__(self, initializer: _Callable[..., resources.Resource[T]], *args: Injection, **kwargs: Injection) -> None: ...
@overload
def __init__(self, initializer: _Callable[..., _Iterator[T]], *args: Injection, **kwargs: Injection) -> None: ...
@overload
def __init__(self, initializer: _Callable[..., T], *args: Injection, **kwargs: Injection) -> None: ...
def __call__(self, *args: Injection, **kwargs: Injection) -> T: ...
@property
def args(self) -> Tuple[Injection]: ...

View File

@ -2,7 +2,7 @@
import abc
import sys
from typing import Generic, TypeVar
from typing import TypeVar, Generic
if sys.version_info < (3, 7):
from typing import GenericMeta

43
tests/typing/resource.py Normal file
View File

@ -0,0 +1,43 @@
from typing import List, Iterator, Generator
from dependency_injector import providers, resources
# Test 1: to check the return type with function
def init1() -> List[int]:
return []
provider1 = providers.Resource(init1)
var1: List[int] = provider1()
# Test 2: to check the return type with iterator
def init2() -> Iterator[List[int]]:
yield []
provider2 = providers.Resource(init2)
var2: List[int] = provider2()
# Test 3: to check the return type with generator
def init3() -> Generator[List[int], None, None]:
yield []
provider3 = providers.Resource(init3)
var3: List[int] = provider3()
# Test 4: to check the return type with resource subclass
class MyResource4(resources.Resource[List[int]]):
def init(self, *args, **kwargs) -> List[int]:
return []
def shutdown(self, resource: List[int]) -> None:
...
provider4 = providers.Resource(MyResource4)
var4: List[int] = provider4()