From a3f04bd34e3d706de6ad24d661bbbb0785434729 Mon Sep 17 00:00:00 2001 From: Roman Mogylatov Date: Fri, 23 Oct 2020 21:34:44 -0400 Subject: [PATCH] Add typing tests --- src/dependency_injector/providers.pyi | 18 +++++------ src/dependency_injector/resources.py | 2 +- tests/typing/resource.py | 43 +++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 tests/typing/resource.py diff --git a/src/dependency_injector/providers.pyi b/src/dependency_injector/providers.pyi index 1a4261e2..8322b6b7 100644 --- a/src/dependency_injector/providers.pyi +++ b/src/dependency_injector/providers.pyi @@ -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]: ... diff --git a/src/dependency_injector/resources.py b/src/dependency_injector/resources.py index 640967fc..34fe6c1c 100644 --- a/src/dependency_injector/resources.py +++ b/src/dependency_injector/resources.py @@ -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 diff --git a/tests/typing/resource.py b/tests/typing/resource.py new file mode 100644 index 00000000..71808b13 --- /dev/null +++ b/tests/typing/resource.py @@ -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()