mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-07-10 16:12:26 +03:00
Add types module for explicit provider typing
This commit is contained in:
parent
d600c6cde4
commit
a2fb095e69
|
@ -14,6 +14,9 @@ from typing import (
|
||||||
Coroutine as _Coroutine,
|
Coroutine as _Coroutine,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from .types import Provider as _Provider
|
||||||
|
|
||||||
|
|
||||||
Injection = Any
|
Injection = Any
|
||||||
T = TypeVar('T')
|
T = TypeVar('T')
|
||||||
|
|
||||||
|
@ -24,7 +27,7 @@ class OverridingContext:
|
||||||
def __exit__(self, *_: Any) -> None: ...
|
def __exit__(self, *_: Any) -> None: ...
|
||||||
|
|
||||||
|
|
||||||
class Provider:
|
class Provider(_Provider):
|
||||||
def __init__(self) -> None: ...
|
def __init__(self) -> None: ...
|
||||||
def __call__(self, *args: Injection, **kwargs: Injection) -> Any: ...
|
def __call__(self, *args: Injection, **kwargs: Injection) -> Any: ...
|
||||||
def __deepcopy__(self, memo: Optional[Dict[str, Any]]) -> Provider: ...
|
def __deepcopy__(self, memo: Optional[Dict[str, Any]]) -> Provider: ...
|
||||||
|
|
9
src/dependency_injector/types.py
Normal file
9
src/dependency_injector/types.py
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
from typing import TypeVar, Generic, Any
|
||||||
|
|
||||||
|
|
||||||
|
Injection = Any
|
||||||
|
T = TypeVar('T')
|
||||||
|
|
||||||
|
|
||||||
|
class Provider(Generic[T]):
|
||||||
|
def __call__(self, *args: Injection, **kwargs: Injection) -> T: ...
|
|
@ -1,6 +1,6 @@
|
||||||
from typing import Tuple, Any, Dict
|
from typing import Tuple, Any, Dict
|
||||||
|
|
||||||
from dependency_injector import providers
|
from dependency_injector import providers, types
|
||||||
|
|
||||||
|
|
||||||
class Animal:
|
class Animal:
|
||||||
|
@ -62,3 +62,7 @@ provider9 = providers.FactoryAggregate(
|
||||||
factory_a_9: providers.Factory = provider9.a
|
factory_a_9: providers.Factory = provider9.a
|
||||||
factory_b_9: providers.Factory = provider9.b
|
factory_b_9: providers.Factory = provider9.b
|
||||||
val9: Any = provider9('a')
|
val9: Any = provider9('a')
|
||||||
|
|
||||||
|
# Test 10: to check the explicit typing
|
||||||
|
factory10: types.Provider[Animal] = providers.Factory(Cat)
|
||||||
|
animal10: Animal = factory10()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user