From df9036b0dc2bb0068a293d63d20fc9fdb2268cf6 Mon Sep 17 00:00:00 2001 From: Roman Mogylatov Date: Tue, 25 Aug 2020 16:39:37 -0400 Subject: [PATCH] Add stub for Dependency provider --- src/dependency_injector/providers.pyi | 9 +++++++++ tests/typing/dependency.py | 22 ++++++++++++++++++++++ tests/typing/factory.py | 1 - 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/typing/dependency.py diff --git a/src/dependency_injector/providers.pyi b/src/dependency_injector/providers.pyi index dd19f9cf..63412c11 100644 --- a/src/dependency_injector/providers.pyi +++ b/src/dependency_injector/providers.pyi @@ -42,6 +42,15 @@ class Delegate(Provider): def __call__(self, *args: Injection, **kwargs: Injection) -> Provider: ... +class Dependency(Provider, Generic[T]): + def __init__(self, instance_of: Type[T]) -> None: ... + def __call__(self, *args: Injection, **kwargs: Injection) -> T: ... + @property + def provided(self) -> ProvidedInstance: ... + @property + def instance_of(self) -> Type[T]: ... + def provided_by(self, provider: Provider) -> OverridingContext: ... + class Callable(Provider, Generic[T]): def __init__(self, provides: _Callable[..., T], *args: Injection, **kwargs: Injection) -> None: ... def __call__(self, *args: Injection, **kwargs: Injection) -> T: ... diff --git a/tests/typing/dependency.py b/tests/typing/dependency.py new file mode 100644 index 00000000..77d6ff08 --- /dev/null +++ b/tests/typing/dependency.py @@ -0,0 +1,22 @@ +from typing import Type + +from dependency_injector import providers + + +class Animal: + ... + + +class Cat(Animal): + + def __init__(self, *_, **__): ... + + +# Test 1: to check the return type +provider1 = providers.Dependency(instance_of=Animal) +provider1.override(providers.Factory(Cat)) +var1: Animal = provider1() + +# Test 2: to check the return type +provider2 = providers.Dependency(instance_of=Animal) +var2: Type[Animal] = provider2.instance_of diff --git a/tests/typing/factory.py b/tests/typing/factory.py index 19693ed7..e89e4a81 100644 --- a/tests/typing/factory.py +++ b/tests/typing/factory.py @@ -4,7 +4,6 @@ from dependency_injector import providers class Animal: - xyz: int = 123 ...