mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 01:26:51 +03:00
Fix type annotations for .provides (#491)
* Fix type annotations for .provides * Fix type hinting for .provides as @rmk135 suggested
This commit is contained in:
parent
cf2861c4b4
commit
b4df3dd2c9
|
@ -84,6 +84,7 @@ class Provider(Generic[T]):
|
||||||
|
|
||||||
class Object(Provider[T]):
|
class Object(Provider[T]):
|
||||||
def __init__(self, provides: Optional[T] = None) -> None: ...
|
def __init__(self, provides: Optional[T] = None) -> None: ...
|
||||||
|
@property
|
||||||
def provides(self) -> Optional[T]: ...
|
def provides(self) -> Optional[T]: ...
|
||||||
def set_provides(self, provides: Optional[T]) -> Object: ...
|
def set_provides(self, provides: Optional[T]) -> Object: ...
|
||||||
|
|
||||||
|
@ -144,7 +145,7 @@ class DependenciesContainer(Object):
|
||||||
class Callable(Provider[T]):
|
class Callable(Provider[T]):
|
||||||
def __init__(self, provides: Optional[_Callable[..., T]] = None, *args: Injection, **kwargs: Injection) -> None: ...
|
def __init__(self, provides: Optional[_Callable[..., T]] = None, *args: Injection, **kwargs: Injection) -> None: ...
|
||||||
@property
|
@property
|
||||||
def provides(self) -> Optional[T]: ...
|
def provides(self) -> Optional[_Callable[..., T]]: ...
|
||||||
def set_provides(self, provides: Optional[_Callable[..., T]]) -> Callable[T]: ...
|
def set_provides(self, provides: Optional[_Callable[..., T]]) -> Callable[T]: ...
|
||||||
@property
|
@property
|
||||||
def args(self) -> Tuple[Injection]: ...
|
def args(self) -> Tuple[Injection]: ...
|
||||||
|
@ -249,9 +250,9 @@ class Factory(Provider[T]):
|
||||||
provided_type: Optional[Type]
|
provided_type: Optional[Type]
|
||||||
def __init__(self, provides: Optional[_Callable[..., T]] = None, *args: Injection, **kwargs: Injection) -> None: ...
|
def __init__(self, provides: Optional[_Callable[..., T]] = None, *args: Injection, **kwargs: Injection) -> None: ...
|
||||||
@property
|
@property
|
||||||
def cls(self) -> T: ...
|
def cls(self) -> Type[T]: ...
|
||||||
@property
|
@property
|
||||||
def provides(self) -> T: ...
|
def provides(self) -> Optional[_Callable[..., T]]: ...
|
||||||
def set_provides(self, provides: Optional[_Callable[..., T]]) -> Factory[T]: ...
|
def set_provides(self, provides: Optional[_Callable[..., T]]) -> Factory[T]: ...
|
||||||
@property
|
@property
|
||||||
def args(self) -> Tuple[Injection]: ...
|
def args(self) -> Tuple[Injection]: ...
|
||||||
|
@ -300,9 +301,9 @@ class BaseSingleton(Provider[T]):
|
||||||
provided_type = Optional[Type]
|
provided_type = Optional[Type]
|
||||||
def __init__(self, provides: Optional[_Callable[..., T]] = None, *args: Injection, **kwargs: Injection) -> None: ...
|
def __init__(self, provides: Optional[_Callable[..., T]] = None, *args: Injection, **kwargs: Injection) -> None: ...
|
||||||
@property
|
@property
|
||||||
def cls(self) -> T: ...
|
def cls(self) -> Type[T]: ...
|
||||||
@property
|
@property
|
||||||
def provides(self) -> T: ...
|
def provides(self) -> Optional[_Callable[..., T]]: ...
|
||||||
def set_provides(self, provides: Optional[_Callable[..., T]]) -> BaseSingleton[T]: ...
|
def set_provides(self, provides: Optional[_Callable[..., T]]) -> BaseSingleton[T]: ...
|
||||||
@property
|
@property
|
||||||
def args(self) -> Tuple[Injection]: ...
|
def args(self) -> Tuple[Injection]: ...
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
from typing import Tuple, Any, Dict
|
from typing import Callable, Optional, Tuple, Any, Dict, Type
|
||||||
|
|
||||||
from dependency_injector import providers
|
from dependency_injector import providers
|
||||||
|
|
||||||
|
@ -56,3 +56,13 @@ provider9 = providers.Callable(Cat)
|
||||||
async def _async9() -> None:
|
async def _async9() -> None:
|
||||||
animal1: Animal = await provider9(1, 2, 3, b='1', c=2, e=0.0) # type: ignore
|
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)
|
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
|
||||||
|
|
|
@ -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
|
# Test 1: to check the return type
|
||||||
provider1 = providers.Delegate(providers.Provider())
|
provider1 = providers.Delegate(providers.Provider())
|
||||||
|
@ -10,3 +11,7 @@ provider2 = providers.Delegate(providers.Provider())
|
||||||
async def _async2() -> None:
|
async def _async2() -> None:
|
||||||
var1: providers.Provider = await provider2() # type: ignore
|
var1: providers.Provider = await provider2() # type: ignore
|
||||||
var2: providers.Provider = await provider2.async_()
|
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
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
from typing import Tuple, Any, Dict
|
from typing import Callable, Optional, Tuple, Any, Dict, Type
|
||||||
|
|
||||||
from dependency_injector import providers
|
from dependency_injector import providers
|
||||||
|
|
||||||
|
@ -72,3 +72,17 @@ provider11 = providers.Factory(Cat)
|
||||||
async def _async11() -> None:
|
async def _async11() -> None:
|
||||||
animal1: Animal = await provider11(1, 2, 3, b='1', c=2, e=0.0) # type: ignore
|
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)
|
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()
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
from typing import Type, Optional
|
||||||
|
|
||||||
from dependency_injector import providers
|
from dependency_injector import providers
|
||||||
|
|
||||||
|
|
||||||
|
@ -17,3 +19,7 @@ provider3 = providers.Object(int(3))
|
||||||
async def _async3() -> None:
|
async def _async3() -> None:
|
||||||
var1: int = await provider3() # type: ignore
|
var1: int = await provider3() # type: ignore
|
||||||
var2: int = await provider3.async_()
|
var2: int = await provider3.async_()
|
||||||
|
|
||||||
|
# Test 4: to check class type from provider
|
||||||
|
provider4 = providers.Object(int('1'))
|
||||||
|
provided_provides: Optional[int] = provider4.provides
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
from typing import Tuple, Any, Dict
|
from typing import Callable, Optional, Tuple, Any, Dict, Type
|
||||||
|
|
||||||
from dependency_injector import providers
|
from dependency_injector import providers
|
||||||
|
|
||||||
|
@ -75,3 +75,17 @@ provider13 = providers.Singleton(Cat)
|
||||||
async def _async13() -> None:
|
async def _async13() -> None:
|
||||||
animal1: Animal = await provider13(1, 2, 3, b='1', c=2, e=0.0) # type: ignore
|
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)
|
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()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user