Refactor `container.apply_container_providers_overridings() to use container.traverse()`

This commit is contained in:
Roman Mogylatov 2021-02-05 08:59:16 -05:00
parent c9ab7d540d
commit c4892af31e
4 changed files with 788 additions and 802 deletions

View File

@ -9,6 +9,8 @@ follows `Semantic versioning`_
Development version
-------------------
- Refactor ``container.apply_container_providers_overridings()`` to use ``container.traverse()``.
This enables deep lazy initialization of ``Container`` providers.
- Add tests for ``Selector`` provider.
- Add tests for ``ProvidedInstance`` and ``MethodCaller`` providers.
- Update Makefile to make Python 3 tests to be a default test command: ``make test``.

File diff suppressed because it is too large Load Diff

View File

@ -268,9 +268,7 @@ class DynamicContainer(Container):
def apply_container_providers_overridings(self):
"""Apply container providers' overridings."""
for provider in self.providers.values():
if not isinstance(provider, providers.Container):
continue
for provider in self.traverse(types=[providers.Container]):
provider.apply_overridings()

View File

@ -135,6 +135,8 @@ class ContainerTests(unittest.TestCase):
provider.override(providers.Object('foo'))
def test_lazy_overriding(self):
# See: https://github.com/ets-labs/python-dependency-injector/issues/354
class D(containers.DeclarativeContainer):
foo = providers.Object("foo")
@ -150,4 +152,26 @@ class ContainerTests(unittest.TestCase):
b = B(d=D())
result = b.a().bar()
self.assertEqual(result, 'foo++')
# See: https://github.com/ets-labs/python-dependency-injector/issues/354
def test_lazy_overriding_deep(self):
# Extended version of test_lazy_overriding()
class D(containers.DeclarativeContainer):
foo = providers.Object("foo")
class C(containers.DeclarativeContainer):
d = providers.DependenciesContainer()
bar = providers.Callable(lambda f: f + "++", d.foo.provided)
class A(containers.DeclarativeContainer):
d = providers.DependenciesContainer()
c = providers.Container(C, d=d)
class B(containers.DeclarativeContainer):
d = providers.DependenciesContainer()
a = providers.Container(A, d=d)
b = B(d=D())
result = b.a().c().bar()
self.assertEqual(result, 'foo++')