diff --git a/docs/containers/copying.rst b/docs/containers/copying.rst index 5f403838..bd109fe8 100644 --- a/docs/containers/copying.rst +++ b/docs/containers/copying.rst @@ -3,7 +3,7 @@ Container copying 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 :lines: 3- :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. 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:: diff --git a/docs/main/changelog.rst b/docs/main/changelog.rst index 550ec442..ecc656bc 100644 --- a/docs/main/changelog.rst +++ b/docs/main/changelog.rst @@ -7,6 +7,10 @@ that were made in every particular version. From version 0.7.6 *Dependency Injector* framework strictly follows `Semantic versioning`_ +4.25.1 +------ +- Amend docs and add another example for ``@containers.copy()`` decorator. + 4.25.0 ------ - Add ``application-multiple-containers-runtime-overriding`` example. This example demonstrates diff --git a/examples/containers/declarative_copy_decorator.py b/examples/containers/declarative_copy_decorator1.py similarity index 100% rename from examples/containers/declarative_copy_decorator.py rename to examples/containers/declarative_copy_decorator1.py diff --git a/examples/containers/declarative_copy_decorator2.py b/examples/containers/declarative_copy_decorator2.py new file mode 100644 index 00000000..8fbc04f9 --- /dev/null +++ b/examples/containers/declarative_copy_decorator2.py @@ -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 diff --git a/src/dependency_injector/__init__.py b/src/dependency_injector/__init__.py index 3f21f93d..5e4629b5 100644 --- a/src/dependency_injector/__init__.py +++ b/src/dependency_injector/__init__.py @@ -1,6 +1,6 @@ """Top-level package.""" -__version__ = '4.25.0' +__version__ = '4.25.1' """Version number. :type: str