Merge branch 'release/4.25.1' into master

This commit is contained in:
Roman Mogylatov 2021-02-19 08:12:43 -05:00
commit 0026f48cb6
5 changed files with 48 additions and 2 deletions

View File

@ -3,7 +3,7 @@ Container copying
You can create declarative container copies using ``@containers.copy()`` decorator. You can create declarative container copies using ``@containers.copy()`` decorator.
.. literalinclude:: ../../examples/containers/declarative_copy_decorator.py .. literalinclude:: ../../examples/containers/declarative_copy_decorator1.py
:language: python :language: python
:lines: 3- :lines: 3-
:emphasize-lines: 18-22 :emphasize-lines: 18-22
@ -11,4 +11,13 @@ You can create declarative container copies using ``@containers.copy()`` decorat
Decorator ``@containers.copy()`` copies providers from source container to destination container. Decorator ``@containers.copy()`` copies providers from source container to destination container.
Destination container provider will replace source provider, if names match. Destination container provider will replace source provider, if names match.
Decorator ``@containers.copy()`` helps you when you create derived declarative containers
from the base one. Base container often keeps default dependencies while derived containers define
overriding providers. Without ``@containers.copy()`` decorator, overridden providers are available
in the derived container, but base class dependencies continue to be bound to the base class providers.
.. literalinclude:: ../../examples/containers/declarative_copy_decorator2.py
:language: python
:lines: 11-
.. disqus:: .. disqus::

View File

@ -7,6 +7,10 @@ that were made in every particular version.
From version 0.7.6 *Dependency Injector* framework strictly From version 0.7.6 *Dependency Injector* framework strictly
follows `Semantic versioning`_ follows `Semantic versioning`_
4.25.1
------
- Amend docs and add another example for ``@containers.copy()`` decorator.
4.25.0 4.25.0
------ ------
- Add ``application-multiple-containers-runtime-overriding`` example. This example demonstrates - Add ``application-multiple-containers-runtime-overriding`` example. This example demonstrates

View File

@ -0,0 +1,33 @@
"""Declarative container provider copying with ``@copy()`` decorator."""
from dependency_injector import containers, providers
class Service:
def __init__(self, dependency: str):
self.dependency = dependency
class Base(containers.DeclarativeContainer):
dependency = providers.Dependency(instance_of=str, default='Default value')
service = providers.Factory(Service, dependency=dependency)
@containers.copy(Base)
class Derived1(Base):
dependency = providers.Dependency(instance_of=str, default='Derived 1')
# @containers.copy(Base) # <-- No @copy decorator
class Derived2(Base):
dependency = providers.Dependency(instance_of=str, default='Derived 2')
if __name__ == '__main__':
container1 = Derived1()
service1 = container1.service()
print(service1.dependency) # Derived 1
container2 = Derived2()
service2 = container2.service()
print(service2.dependency) # Default value

View File

@ -1,6 +1,6 @@
"""Top-level package.""" """Top-level package."""
__version__ = '4.25.0' __version__ = '4.25.1'
"""Version number. """Version number.
:type: str :type: str