diff --git a/src/dependency_injector/providers.pyi b/src/dependency_injector/providers.pyi index 05234c85..66e34e3e 100644 --- a/src/dependency_injector/providers.pyi +++ b/src/dependency_injector/providers.pyi @@ -158,10 +158,10 @@ class DependenciesContainer(Object): class Callable(Provider[T]): - def __init__(self, provides: Optional[_Callable[..., T]] = None, *args: Injection, **kwargs: Injection) -> None: ... + def __init__(self, provides: Optional[Union[_Callable[..., T], str]] = None, *args: Injection, **kwargs: Injection) -> None: ... @property def provides(self) -> Optional[_Callable[..., T]]: ... - def set_provides(self, provides: Optional[_Callable[..., T]]) -> Callable[T]: ... + def set_provides(self, provides: Optional[Union[_Callable[..., T], str]]) -> Callable[T]: ... @property def args(self) -> Tuple[Injection]: ... def add_args(self, *args: Injection) -> Callable[T]: ... @@ -283,12 +283,12 @@ class Configuration(Object[Any]): class Factory(Provider[T]): provided_type: Optional[Type] - def __init__(self, provides: Optional[_Callable[..., T]] = None, *args: Injection, **kwargs: Injection) -> None: ... + def __init__(self, provides: Optional[Union[_Callable[..., T], str]] = None, *args: Injection, **kwargs: Injection) -> None: ... @property def cls(self) -> Type[T]: ... @property def provides(self) -> Optional[_Callable[..., T]]: ... - def set_provides(self, provides: Optional[_Callable[..., T]]) -> Factory[T]: ... + def set_provides(self, provides: Optional[Union[_Callable[..., T], str]]) -> Factory[T]: ... @property def args(self) -> Tuple[Injection]: ... def add_args(self, *args: Injection) -> Factory[T]: ... @@ -326,12 +326,12 @@ class FactoryAggregate(Aggregate[T]): class BaseSingleton(Provider[T]): provided_type = Optional[Type] - def __init__(self, provides: Optional[_Callable[..., T]] = None, *args: Injection, **kwargs: Injection) -> None: ... + def __init__(self, provides: Optional[Union[_Callable[..., T], str]] = None, *args: Injection, **kwargs: Injection) -> None: ... @property def cls(self) -> Type[T]: ... @property def provides(self) -> Optional[_Callable[..., T]]: ... - def set_provides(self, provides: Optional[_Callable[..., T]]) -> BaseSingleton[T]: ... + def set_provides(self, provides: Optional[Union[_Callable[..., T], str]]) -> BaseSingleton[T]: ... @property def args(self) -> Tuple[Injection]: ... def add_args(self, *args: Injection) -> BaseSingleton[T]: ... @@ -377,7 +377,7 @@ class AbstractSingleton(BaseSingleton[T]): class SingletonDelegate(Delegate): - def __init__(self, factory: BaseSingleton): ... + def __init__(self, singleton: BaseSingleton): ... class List(Provider[_List]): @@ -410,7 +410,7 @@ class Resource(Provider[T]): @overload def __init__(self, provides: Optional[_Callable[..., _Coroutine[Injection, Injection, T]]] = None, *args: Injection, **kwargs: Injection) -> None: ... @overload - def __init__(self, provides: Optional[_Callable[..., T]] = None, *args: Injection, **kwargs: Injection) -> None: ... + def __init__(self, provides: Optional[Union[_Callable[..., T], str]] = None, *args: Injection, **kwargs: Injection) -> None: ... @property def provides(self) -> Optional[_Callable[..., Any]]: ... def set_provides(self, provides: Optional[Any]) -> Resource[T]: ... diff --git a/tests/typing/callable.py b/tests/typing/callable.py index 09cdf9bc..51c8d3a7 100644 --- a/tests/typing/callable.py +++ b/tests/typing/callable.py @@ -66,3 +66,7 @@ assert provides10 is Cat provider11 = providers.Callable[Animal](Cat) provides11: Optional[Callable[..., Animal]] = provider11.provides assert provides11 is Cat + +# Test 12: to check string imports +provider12: providers.Callable[dict] = providers.Callable("builtins.dict") +provider12.set_provides("builtins.dict") diff --git a/tests/typing/coroutine.py b/tests/typing/coroutine.py index 839698dd..40c8e530 100644 --- a/tests/typing/coroutine.py +++ b/tests/typing/coroutine.py @@ -9,3 +9,7 @@ async def _coro() -> None: # Test 1: to check the return type provider1 = providers.Coroutine(_coro) var1: Coroutine = provider1() + +# Test 2: to check string imports +provider2: providers.Coroutine[None] = providers.Coroutine("_coro") +provider2.set_provides("_coro") diff --git a/tests/typing/factory.py b/tests/typing/factory.py index 61999e2f..132a4c29 100644 --- a/tests/typing/factory.py +++ b/tests/typing/factory.py @@ -99,3 +99,7 @@ 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() + +# Test 14: to check string imports +provider14: providers.Factory[dict] = providers.Factory("builtins.dict") +provider14.set_provides("builtins.dict") diff --git a/tests/typing/resource.py b/tests/typing/resource.py index b468df42..e70ae67a 100644 --- a/tests/typing/resource.py +++ b/tests/typing/resource.py @@ -97,3 +97,8 @@ provider8 = providers.Resource(MyResource8) async def _provide8() -> None: var1: List[int] = await provider8() # type: ignore var2: List[int] = await provider8.async_() + + +# Test 9: to check string imports +provider9: providers.Resource[dict] = providers.Resource("builtins.dict") +provider9.set_provides("builtins.dict") diff --git a/tests/typing/singleton.py b/tests/typing/singleton.py index b19a43fe..badfe1c6 100644 --- a/tests/typing/singleton.py +++ b/tests/typing/singleton.py @@ -89,3 +89,7 @@ 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() + +# Test 16: to check string imports +provider16: providers.Singleton[dict] = providers.Singleton("builtins.dict") +provider16.set_provides("builtins.dict")