mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
Move "provided" attribute (#306)
* Update typing stub * Move attribute * Add typing test * Update docs * Add test * Update changelog
This commit is contained in:
parent
452a13c9f7
commit
ff4d24706e
|
@ -9,6 +9,7 @@ follows `Semantic versioning`_
|
|||
|
||||
Develop
|
||||
-------
|
||||
- Move ``.provided`` attribute to ``providers.Provider``.
|
||||
- Update all links in documentation and examples to use ``https://`` instead of ``http``.
|
||||
|
||||
4.0.4
|
||||
|
|
|
@ -35,24 +35,4 @@ You can do nested constructions:
|
|||
:emphasize-lines: 26-32
|
||||
:lines: 3-
|
||||
|
||||
The ``.provided`` attribute is available for the next providers:
|
||||
|
||||
- :py:class:`Factory` and its subclasses
|
||||
- :py:class:`Singleton` and its subclasses
|
||||
- :py:class:`Callable` and its subclasses
|
||||
- :py:class:`Object`
|
||||
- :py:class:`List`
|
||||
- :py:class:`Selector`
|
||||
- :py:class:`Dependency`
|
||||
|
||||
When you create a new provider subclass and want to implement the ``.provided`` attribute, you
|
||||
should use the :py:class:`ProvidedInstance` provider. Add the ``.provided`` property
|
||||
implementation to a new subclass:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
@property
|
||||
def provided(self):
|
||||
return ProvidedInstance(self)
|
||||
|
||||
.. disqus::
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -42,14 +42,14 @@ class Provider(Generic[T]):
|
|||
def delegate(self) -> Provider: ...
|
||||
@property
|
||||
def provider(self) -> Provider: ...
|
||||
@property
|
||||
def provided(self) -> ProvidedInstance: ...
|
||||
def _copy_overridings(self, copied: Provider, memo: Optional[Dict[Any, Any]]) -> None: ...
|
||||
|
||||
|
||||
class Object(Provider, Generic[T]):
|
||||
def __init__(self, provides: T) -> None: ...
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> T: ...
|
||||
@property
|
||||
def provided(self) -> ProvidedInstance: ...
|
||||
|
||||
|
||||
class Delegate(Provider):
|
||||
|
@ -63,8 +63,6 @@ class Dependency(Provider, Generic[T]):
|
|||
def __init__(self, instance_of: Type[T] = object) -> 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: ...
|
||||
|
||||
|
@ -85,8 +83,6 @@ class Callable(Provider, Generic[T]):
|
|||
@property
|
||||
def provides(self) -> T: ...
|
||||
@property
|
||||
def provided(self) -> ProvidedInstance: ...
|
||||
@property
|
||||
def args(self) -> Tuple[Injection]: ...
|
||||
def add_args(self, *args: Injection) -> Callable[T]: ...
|
||||
def set_args(self, *args: Injection) -> Callable[T]: ...
|
||||
|
@ -173,8 +169,6 @@ class Factory(Provider, Generic[T]):
|
|||
@property
|
||||
def provides(self) -> T: ...
|
||||
@property
|
||||
def provided(self) -> ProvidedInstance: ...
|
||||
@property
|
||||
def args(self) -> Tuple[Injection]: ...
|
||||
def add_args(self, *args: Injection) -> Factory[T]: ...
|
||||
def set_args(self, *args: Injection) -> Factory[T]: ...
|
||||
|
@ -217,8 +211,6 @@ class BaseSingleton(Provider, Generic[T]):
|
|||
@property
|
||||
def cls(self) -> T: ...
|
||||
@property
|
||||
def provided(self) -> ProvidedInstance: ...
|
||||
@property
|
||||
def args(self) -> Tuple[Injection]: ...
|
||||
def add_args(self, *args: Injection) -> Factory[T]: ...
|
||||
def set_args(self, *args: Injection) -> Factory[T]: ...
|
||||
|
@ -266,8 +258,6 @@ class List(Provider):
|
|||
def __init__(self, *args: Injection): ...
|
||||
def __call__(self, *args: Injection, **kwargs: Injection) -> _List[Any]: ...
|
||||
@property
|
||||
def provided(self) -> ProvidedInstance: ...
|
||||
@property
|
||||
def args(self) -> Tuple[Injection]: ...
|
||||
def add_args(self, *args: Injection) -> List: ...
|
||||
def set_args(self, *args: Injection) -> List: ...
|
||||
|
@ -288,8 +278,6 @@ class Selector(Provider):
|
|||
def __call__(self, *args: Injection, **kwargs: Injection) -> Any: ...
|
||||
def __getattr__(self, name: str) -> Provider: ...
|
||||
@property
|
||||
def provided(self) -> ProvidedInstance: ...
|
||||
@property
|
||||
def providers(self) -> Dict[str, Provider]: ...
|
||||
|
||||
|
||||
|
|
|
@ -273,6 +273,11 @@ cdef class Provider(object):
|
|||
"""
|
||||
return Delegate(self)
|
||||
|
||||
@property
|
||||
def provided(self):
|
||||
"""Return :py:class:`ProvidedInstance` provider."""
|
||||
return ProvidedInstance(self)
|
||||
|
||||
cpdef object _provide(self, tuple args, dict kwargs):
|
||||
"""Providing strategy implementation.
|
||||
|
||||
|
@ -333,11 +338,6 @@ cdef class Object(Provider):
|
|||
"""
|
||||
return self.__str__()
|
||||
|
||||
@property
|
||||
def provided(self):
|
||||
"""Return :py:class:`ProvidedInstance` provider."""
|
||||
return ProvidedInstance(self)
|
||||
|
||||
cpdef object _provide(self, tuple args, dict kwargs):
|
||||
"""Return provided instance.
|
||||
|
||||
|
@ -498,11 +498,6 @@ cdef class Dependency(Provider):
|
|||
"""
|
||||
return self.__str__()
|
||||
|
||||
@property
|
||||
def provided(self):
|
||||
"""Return :py:class:`ProvidedInstance` provider."""
|
||||
return ProvidedInstance(self)
|
||||
|
||||
@property
|
||||
def instance_of(self):
|
||||
"""Return class of required dependency."""
|
||||
|
@ -793,11 +788,6 @@ cdef class Callable(Provider):
|
|||
"""Return wrapped callable."""
|
||||
return self.__provides
|
||||
|
||||
@property
|
||||
def provided(self):
|
||||
"""Return :py:class:`ProvidedInstance` provider."""
|
||||
return ProvidedInstance(self)
|
||||
|
||||
@property
|
||||
def args(self):
|
||||
"""Return positional argument injections."""
|
||||
|
@ -1651,11 +1641,6 @@ cdef class Factory(Provider):
|
|||
"""Return provided type."""
|
||||
return self.__instantiator.provides
|
||||
|
||||
@property
|
||||
def provided(self):
|
||||
"""Return :py:class:`ProvidedInstance` provider."""
|
||||
return ProvidedInstance(self)
|
||||
|
||||
@property
|
||||
def args(self):
|
||||
"""Return positional argument injections."""
|
||||
|
@ -1989,11 +1974,6 @@ cdef class BaseSingleton(Provider):
|
|||
"""Return provided type."""
|
||||
return self.__instantiator.cls
|
||||
|
||||
@property
|
||||
def provided(self):
|
||||
"""Return :py:class:`ProvidedInstance` provider."""
|
||||
return ProvidedInstance(self)
|
||||
|
||||
@property
|
||||
def args(self):
|
||||
"""Return positional argument injections."""
|
||||
|
@ -2422,11 +2402,6 @@ cdef class List(Provider):
|
|||
"""
|
||||
return represent_provider(provider=self, provides=list(self.args))
|
||||
|
||||
@property
|
||||
def provided(self):
|
||||
"""Return :py:class:`ProvidedInstance` provider."""
|
||||
return ProvidedInstance(self)
|
||||
|
||||
@property
|
||||
def args(self):
|
||||
"""Return positional argument injections."""
|
||||
|
@ -2608,11 +2583,6 @@ cdef class Selector(Provider):
|
|||
address=hex(id(self)),
|
||||
)
|
||||
|
||||
@property
|
||||
def provided(self):
|
||||
"""Return :py:class:`ProvidedInstance` provider."""
|
||||
return ProvidedInstance(self)
|
||||
|
||||
@property
|
||||
def providers(self):
|
||||
"""Return providers."""
|
||||
|
|
6
tests/typing/provider.py
Normal file
6
tests/typing/provider.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from dependency_injector import providers
|
||||
|
||||
|
||||
# Test 1: to check .provided attribute
|
||||
provider1: providers.Provider[int] = providers.Object(1)
|
||||
provided: providers.ProvidedInstance = provider1.provided
|
|
@ -137,3 +137,10 @@ class ProvidedInstancePuzzleTests(unittest.TestCase):
|
|||
'foo-bar',
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
class ProvidedInstanceInBaseClassTests(unittest.TestCase):
|
||||
|
||||
def test_provided_attribute(self):
|
||||
provider = providers.Provider()
|
||||
assert isinstance(provider.provided, providers.ProvidedInstance)
|
||||
|
|
Loading…
Reference in New Issue
Block a user