diff --git a/docs/containers/copying.rst b/docs/containers/copying.rst index bd109fe8..29c0d2ad 100644 --- a/docs/containers/copying.rst +++ b/docs/containers/copying.rst @@ -10,6 +10,7 @@ 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. +Dependencies on the source container's ``Self`` provider refer to the destination container. 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 diff --git a/src/dependency_injector/containers.pyx b/src/dependency_injector/containers.pyx index cb55083f..bdb28fc2 100644 --- a/src/dependency_injector/containers.pyx +++ b/src/dependency_injector/containers.pyx @@ -883,7 +883,10 @@ def copy(object base_container): return memo def _decorator(new_container): - memo = {} + memo = { + id(base_container.__self__): new_container.__self__, + id(new_container.__self__): new_container.__self__, + } memo.update(_get_memo_for_matching_names(new_container.cls_providers, base_container.providers)) new_providers = {} diff --git a/tests/unit/containers/cls/test_copy_self_py2_py3.py b/tests/unit/containers/cls/test_copy_self_py2_py3.py new file mode 100644 index 00000000..1b48159e --- /dev/null +++ b/tests/unit/containers/cls/test_copy_self_py2_py3.py @@ -0,0 +1,133 @@ +"""Regression tests for Self dependencies in copied containers.""" + +from dependency_injector import containers, providers + + +def test_copy_rebinds_base_self_and_preserves_source_instances(): + class Base(containers.DeclarativeContainer): + __self__ = providers.Self() + value = providers.Object("base") + consumer = providers.Singleton(dict, container=__self__) + + @containers.copy(Base) + class Copied(Base): + value = providers.Object("copy") + + original = Base() + first = Copied() + second = Copied() + for instance in (original, first, second): + assert instance.consumer()["container"] is instance + assert instance.__self__() is instance + assert original.consumer()["container"].value() == "base" + assert first.consumer()["container"].value() == "copy" + assert first.consumer() is not second.consumer() + assert Base.consumer()["container"] is Base + assert Copied.consumer()["container"] is Copied + + +def test_copy_rebinds_target_self_and_its_aliases(): + class Base(containers.DeclarativeContainer): + __self__ = providers.Self() + inherited = providers.Factory(dict, container=__self__) + + @containers.copy(Base) + class Copied(Base): + this = providers.Self() + also_this = this + own = providers.Factory(dict, container=this) + + instance = Copied() + assert instance.inherited()["container"] is instance + assert instance.own()["container"] is instance + assert instance.this() is instance + assert instance.also_this is instance.this is instance.__self__ + assert "this" not in instance.providers + assert "also_this" not in instance.providers + assert Base.__self__() is Base + + +def test_copy_rebinds_self_through_delegated_and_provided_dependencies(): + class Base(containers.DeclarativeContainer): + __self__ = providers.Self() + value = providers.Object("base") + delegated = providers.Factory(dict, provider=__self__.provider) + provided = providers.Callable(str, __self__.provided.value.call()) + + @containers.copy(Base) + class Copied(Base): + value = providers.Object("copy") + + original = Base() + instance = Copied() + assert instance.delegated()["provider"]() is instance + assert instance.provided() == "copy" + assert original.delegated()["provider"]() is original + assert original.provided() == "base" + + +def test_repeated_copy_and_deepcopy_keep_the_current_container(): + class Base(containers.DeclarativeContainer): + __self__ = providers.Self() + consumer = providers.Factory(dict, container=__self__) + + @containers.copy(Base) + class First(Base): + pass + + @containers.copy(First) + class Second(First): + pass + + instances = [Base(), First(), Second()] + instances.append(providers.deepcopy(instances[-1])) + for instance in instances: + assert instance.consumer()["container"] is instance + + +def test_copy_uses_self_for_resource_lifecycle(): + initialized = [] + stopped = [] + + def init_resource(owner): + initialized.append(owner) + yield owner + stopped.append(owner) + + class Base(containers.DeclarativeContainer): + __self__ = providers.Self() + resource = providers.Resource(init_resource, __self__) + + @containers.copy(Base) + class Copied(Base): + pass + + instance = Copied() + instance.init_resources() + assert initialized == [instance] + assert instance.resource() is instance + instance.shutdown_resources() + assert stopped == [instance] + assert not Base.resource.initialized + + +def test_copy_keeps_self_of_an_independent_nested_container(): + class Child(containers.DeclarativeContainer): + __self__ = providers.Self() + consumer = providers.Factory(dict, container=__self__) + + class Base(containers.DeclarativeContainer): + __self__ = providers.Self() + child = providers.Container(Child) + consumer = providers.Factory(dict, container=__self__, child=child) + + @containers.copy(Base) + class Copied(Base): + pass + + instance = Copied() + child = instance.child() + assert instance.consumer()["container"] is instance + assert instance.consumer()["child"] is child + assert child.consumer()["container"] is child + assert child.__self__() is child