Skip to content

Commit b4df3dd

Browse files
authored
Fix type annotations for .provides (#491)
* Fix type annotations for .provides * Fix type hinting for .provides as @rmk135 suggested
1 parent cf2861c commit b4df3dd

File tree

6 files changed

+59
-9
lines changed

6 files changed

+59
-9
lines changed

src/dependency_injector/providers.pyi

+6-5
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class Provider(Generic[T]):
8484

8585
class Object(Provider[T]):
8686
def __init__(self, provides: Optional[T] = None) -> None: ...
87+
@property
8788
def provides(self) -> Optional[T]: ...
8889
def set_provides(self, provides: Optional[T]) -> Object: ...
8990

@@ -144,7 +145,7 @@ class DependenciesContainer(Object):
144145
class Callable(Provider[T]):
145146
def __init__(self, provides: Optional[_Callable[..., T]] = None, *args: Injection, **kwargs: Injection) -> None: ...
146147
@property
147-
def provides(self) -> Optional[T]: ...
148+
def provides(self) -> Optional[_Callable[..., T]]: ...
148149
def set_provides(self, provides: Optional[_Callable[..., T]]) -> Callable[T]: ...
149150
@property
150151
def args(self) -> Tuple[Injection]: ...
@@ -249,9 +250,9 @@ class Factory(Provider[T]):
249250
provided_type: Optional[Type]
250251
def __init__(self, provides: Optional[_Callable[..., T]] = None, *args: Injection, **kwargs: Injection) -> None: ...
251252
@property
252-
def cls(self) -> T: ...
253+
def cls(self) -> Type[T]: ...
253254
@property
254-
def provides(self) -> T: ...
255+
def provides(self) -> Optional[_Callable[..., T]]: ...
255256
def set_provides(self, provides: Optional[_Callable[..., T]]) -> Factory[T]: ...
256257
@property
257258
def args(self) -> Tuple[Injection]: ...
@@ -300,9 +301,9 @@ class BaseSingleton(Provider[T]):
300301
provided_type = Optional[Type]
301302
def __init__(self, provides: Optional[_Callable[..., T]] = None, *args: Injection, **kwargs: Injection) -> None: ...
302303
@property
303-
def cls(self) -> T: ...
304+
def cls(self) -> Type[T]: ...
304305
@property
305-
def provides(self) -> T: ...
306+
def provides(self) -> Optional[_Callable[..., T]]: ...
306307
def set_provides(self, provides: Optional[_Callable[..., T]]) -> BaseSingleton[T]: ...
307308
@property
308309
def args(self) -> Tuple[Injection]: ...

tests/typing/callable.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Tuple, Any, Dict
1+
from typing import Callable, Optional, Tuple, Any, Dict, Type
22

33
from dependency_injector import providers
44

@@ -56,3 +56,13 @@ def create(cls) -> Animal:
5656
async def _async9() -> None:
5757
animal1: Animal = await provider9(1, 2, 3, b='1', c=2, e=0.0) # type: ignore
5858
animal2: Animal = await provider9.async_(1, 2, 3, b='1', c=2, e=0.0)
59+
60+
# Test 10: to check the .provides
61+
provider10 = providers.Callable(Cat)
62+
provides10: Optional[Callable[..., Cat]] = provider10.provides
63+
assert provides10 is Cat
64+
65+
# Test 11: to check the .provides for explicit typevar
66+
provider11 = providers.Callable[Animal](Cat)
67+
provides11: Optional[Callable[..., Animal]] = provider11.provides
68+
assert provides11 is Cat

tests/typing/delegate.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from dependency_injector import providers
1+
from typing import Optional
22

3+
from dependency_injector import providers
34

45
# Test 1: to check the return type
56
provider1 = providers.Delegate(providers.Provider())
@@ -10,3 +11,7 @@
1011
async def _async2() -> None:
1112
var1: providers.Provider = await provider2() # type: ignore
1213
var2: providers.Provider = await provider2.async_()
14+
15+
# Test 3: to check class type from provider
16+
provider3 = providers.Delegate(providers.Provider())
17+
provided_provides: Optional[providers.Provider] = provider3.provides

tests/typing/factory.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Tuple, Any, Dict
1+
from typing import Callable, Optional, Tuple, Any, Dict, Type
22

33
from dependency_injector import providers
44

@@ -72,3 +72,17 @@ def create(cls) -> Animal:
7272
async def _async11() -> None:
7373
animal1: Animal = await provider11(1, 2, 3, b='1', c=2, e=0.0) # type: ignore
7474
animal2: Animal = await provider11.async_(1, 2, 3, b='1', c=2, e=0.0)
75+
76+
# Test 12: to check class type from .provides
77+
provider12 = providers.Factory(Cat)
78+
provided_cls12: Type[Animal] = provider12.cls
79+
assert issubclass(provided_cls12, Animal)
80+
provided_provides12: Optional[Callable[..., Animal]] = provider12.provides
81+
assert provided_provides12 is not None and provided_provides12() == Cat()
82+
83+
# Test 13: to check class from .provides with explicit typevar
84+
provider13 = providers.Factory[Animal](Cat)
85+
provided_cls13: Type[Animal] = provider13.cls
86+
assert issubclass(provided_cls13, Animal)
87+
provided_provides13: Optional[Callable[..., Animal]] = provider13.provides
88+
assert provided_provides13 is not None and provided_provides13() == Cat()

tests/typing/object.py

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Type, Optional
2+
13
from dependency_injector import providers
24

35

@@ -17,3 +19,7 @@
1719
async def _async3() -> None:
1820
var1: int = await provider3() # type: ignore
1921
var2: int = await provider3.async_()
22+
23+
# Test 4: to check class type from provider
24+
provider4 = providers.Object(int('1'))
25+
provided_provides: Optional[int] = provider4.provides

tests/typing/singleton.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Tuple, Any, Dict
1+
from typing import Callable, Optional, Tuple, Any, Dict, Type
22

33
from dependency_injector import providers
44

@@ -75,3 +75,17 @@ def create(cls) -> Animal:
7575
async def _async13() -> None:
7676
animal1: Animal = await provider13(1, 2, 3, b='1', c=2, e=0.0) # type: ignore
7777
animal2: Animal = await provider13.async_(1, 2, 3, b='1', c=2, e=0.0)
78+
79+
# Test 14: to check class from .provides
80+
provider14 = providers.Singleton(Cat)
81+
provided_cls14: Type[Cat] = provider14.cls
82+
assert issubclass(provided_cls14, Cat)
83+
provided_provides14: Optional[Callable[..., Cat]] = provider14.provides
84+
assert provided_provides14 is not None and provided_provides14() == Cat()
85+
86+
# Test 15: to check class from .provides with explicit typevar
87+
provider15 = providers.Singleton[Animal](Cat)
88+
provided_cls15: Type[Animal] = provider15.cls
89+
assert issubclass(provided_cls15, Animal)
90+
provided_provides15: Optional[Callable[..., Animal]] = provider15.provides
91+
assert provided_provides15 is not None and provided_provides15() == Cat()

0 commit comments

Comments
 (0)