379 Fix a bug in `providers.Container` when it's declared not at class root level

This commit is contained in:
Roman Mogylatov 2021-01-27 14:01:33 -05:00
parent 874b13fdea
commit ba0fb38ad0
4 changed files with 1268 additions and 1174 deletions

View File

@ -7,6 +7,12 @@ 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`_
Development version
-------------------
- Fix a bug in ``providers.Container`` when it's declared not at class root level.
See issue `#379 <https://github.com/ets-labs/python-dependency-injector/issues/379>`_.
Many thanks to `@approxit <https://github.com/approxit>`_ for reporting the issue.
4.11.1 4.11.1
------ ------
- Fix a bug in ``@containers.copy`` to improve replacing of subcontainer providers. - Fix a bug in ``@containers.copy`` to improve replacing of subcontainer providers.

File diff suppressed because it is too large Load Diff

View File

@ -3199,6 +3199,8 @@ cdef class Container(Provider):
container = container_cls() container = container_cls()
self.__container = container self.__container = container
self.apply_overridings()
super(Container, self).__init__() super(Container, self).__init__()
def __deepcopy__(self, memo): def __deepcopy__(self, memo):

View File

@ -54,6 +54,7 @@ class ContainerTests(unittest.TestCase):
self.assertEqual(application.dict_factory(), {'value': TEST_VALUE_2}) self.assertEqual(application.dict_factory(), {'value': TEST_VALUE_2})
def test_override(self): def test_override(self):
# See: https://github.com/ets-labs/python-dependency-injector/issues/354
class D(containers.DeclarativeContainer): class D(containers.DeclarativeContainer):
foo = providers.Object('foo') foo = providers.Object('foo')
@ -69,7 +70,63 @@ class ContainerTests(unittest.TestCase):
b = B(d=D()) b = B(d=D())
result = b.a().bar() result = b.a().bar()
self.assertEqual(result, 'foo++') self.assertEqual(result, 'foo++')
# See: https://github.com/ets-labs/python-dependency-injector/issues/354
def test_override_not_root_provider(self):
# See: https://github.com/ets-labs/python-dependency-injector/issues/379
class NestedContainer(containers.DeclarativeContainer):
settings = providers.Configuration()
print_settings = providers.Callable(
lambda s: s,
settings,
)
class TestContainer(containers.DeclarativeContainer):
settings = providers.Configuration()
root_container = providers.Container(
NestedContainer,
settings=settings,
)
not_root_container = providers.Selector(
settings.container,
using_factory=providers.Factory(
NestedContainer,
settings=settings,
),
using_container=providers.Container(
NestedContainer,
settings=settings,
)
)
container_using_factory = TestContainer(settings=dict(
container='using_factory',
foo='bar'
))
self.assertEqual(
container_using_factory.root_container().print_settings(),
{'container': 'using_factory', 'foo': 'bar'},
)
self.assertEqual(
container_using_factory.not_root_container().print_settings(),
{'container': 'using_factory', 'foo': 'bar'},
)
container_using_container = TestContainer(settings=dict(
container='using_container',
foo='bar'
))
self.assertEqual(
container_using_container.root_container().print_settings(),
{'container': 'using_container', 'foo': 'bar'},
)
self.assertEqual(
container_using_container.not_root_container().print_settings(),
{'container': 'using_container', 'foo': 'bar'},
)
def test_override_by_not_a_container(self): def test_override_by_not_a_container(self):
provider = providers.Container(TestCore) provider = providers.Container(TestCore)