From a85d89e6f22eb9f89a6820ac8758b756040acf3f Mon Sep 17 00:00:00 2001 From: Roman Mogylatov Date: Fri, 19 Feb 2021 08:11:05 -0500 Subject: [PATCH 1/2] Amend docs and add another example for ``@containers.copy()`` decorator --- docs/containers/copying.rst | 11 ++++++- ...ator.py => declarative_copy_decorator1.py} | 0 .../containers/declarative_copy_decorator2.py | 33 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) rename examples/containers/{declarative_copy_decorator.py => declarative_copy_decorator1.py} (100%) create mode 100644 examples/containers/declarative_copy_decorator2.py 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/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 From e5017347c76cd4de938d73e2f81c1003db99a6f8 Mon Sep 17 00:00:00 2001 From: Roman Mogylatov Date: Fri, 19 Feb 2021 08:11:18 -0500 Subject: [PATCH 2/2] Bump version to 4.25.1 --- docs/main/changelog.rst | 4 ++++ src/dependency_injector/__init__.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) 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/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