From b4df3dd2c9f77499b458f03cd1a307c6581d6626 Mon Sep 17 00:00:00 2001 From: Thiago Hiromi Date: Tue, 24 Aug 2021 03:24:36 +0200 Subject: [PATCH] Fix type annotations for .provides (#491) * Fix type annotations for .provides * Fix type hinting for .provides as @rmk135 suggested --- src/dependency_injector/providers.pyi | 11 ++++++----- tests/typing/callable.py | 12 +++++++++++- tests/typing/delegate.py | 7 ++++++- tests/typing/factory.py | 16 +++++++++++++++- tests/typing/object.py | 6 ++++++ tests/typing/singleton.py | 16 +++++++++++++++- 6 files changed, 59 insertions(+), 9 deletions(-) diff --git a/src/dependency_injector/providers.pyi b/src/dependency_injector/providers.pyi index abb70ba5..17eee7a8 100644 --- a/src/dependency_injector/providers.pyi +++ b/src/dependency_injector/providers.pyi @@ -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) -> Optional[_Callable[..., 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) -> Optional[_Callable[..., 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) -> Optional[_Callable[..., T]]: ... def set_provides(self, provides: Optional[_Callable[..., T]]) -> BaseSingleton[T]: ... @property def args(self) -> Tuple[Injection]: ... diff --git a/tests/typing/callable.py b/tests/typing/callable.py index 1438f9ac..46966f9f 100644 --- a/tests/typing/callable.py +++ b/tests/typing/callable.py @@ -1,4 +1,4 @@ -from typing import Tuple, Any, Dict +from typing import Callable, Optional, Tuple, Any, Dict, Type from dependency_injector import providers @@ -56,3 +56,13 @@ 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 +provider10 = providers.Callable(Cat) +provides10: Optional[Callable[..., Cat]] = provider10.provides +assert provides10 is Cat + +# Test 11: to check the .provides for explicit typevar +provider11 = providers.Callable[Animal](Cat) +provides11: Optional[Callable[..., Animal]] = provider11.provides +assert provides11 is Cat diff --git a/tests/typing/delegate.py b/tests/typing/delegate.py index e934cb5c..b4fb7d33 100644 --- a/tests/typing/delegate.py +++ b/tests/typing/delegate.py @@ -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 diff --git a/tests/typing/factory.py b/tests/typing/factory.py index d4224811..963eb37c 100644 --- a/tests/typing/factory.py +++ b/tests/typing/factory.py @@ -1,4 +1,4 @@ -from typing import Tuple, Any, Dict +from typing import Callable, Optional, Tuple, Any, Dict, Type from dependency_injector import providers @@ -72,3 +72,17 @@ 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 .provides +provider12 = providers.Factory(Cat) +provided_cls12: Type[Animal] = provider12.cls +assert issubclass(provided_cls12, Animal) +provided_provides12: Optional[Callable[..., Animal]] = provider12.provides +assert provided_provides12 is not None and provided_provides12() == Cat() + +# Test 13: to check class from .provides with explicit typevar +provider13 = providers.Factory[Animal](Cat) +provided_cls13: Type[Animal] = provider13.cls +assert issubclass(provided_cls13, Animal) +provided_provides13: Optional[Callable[..., Animal]] = provider13.provides +assert provided_provides13 is not None and provided_provides13() == Cat() diff --git a/tests/typing/object.py b/tests/typing/object.py index 05829f05..f97ef57b 100644 --- a/tests/typing/object.py +++ b/tests/typing/object.py @@ -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 diff --git a/tests/typing/singleton.py b/tests/typing/singleton.py index 667edda6..11276c6e 100644 --- a/tests/typing/singleton.py +++ b/tests/typing/singleton.py @@ -1,4 +1,4 @@ -from typing import Tuple, Any, Dict +from typing import Callable, Optional, Tuple, Any, Dict, Type from dependency_injector import providers @@ -75,3 +75,17 @@ 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 .provides +provider14 = providers.Singleton(Cat) +provided_cls14: Type[Cat] = provider14.cls +assert issubclass(provided_cls14, Cat) +provided_provides14: Optional[Callable[..., Cat]] = provider14.provides +assert provided_provides14 is not None and provided_provides14() == Cat() + +# Test 15: to check class from .provides with explicit typevar +provider15 = providers.Singleton[Animal](Cat) +provided_cls15: Type[Animal] = provider15.cls +assert issubclass(provided_cls15, Animal) +provided_provides15: Optional[Callable[..., Animal]] = provider15.provides +assert provided_provides15 is not None and provided_provides15() == Cat()