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

View File

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