Merge branch 'release/4.11.1' into master

This commit is contained in:
Roman Mogylatov 2021-01-27 09:22:11 -05:00
commit 874b13fdea
5 changed files with 1007 additions and 497 deletions

View File

@ -7,6 +7,12 @@ that were made in every particular version.
From version 0.7.6 *Dependency Injector* framework strictly
follows `Semantic versioning`_
4.11.1
------
- Fix a bug in ``@containers.copy`` to improve replacing of subcontainer providers.
See issue `#378 <https://github.com/ets-labs/python-dependency-injector/issues/378>`_.
Many thanks to `Shaun Cutts <https://github.com/shaunc>`_ for reporting the issue.
4.11.0
------
- Add ``loader`` argument to the configuration provider ``Configuration.from_yaml(..., loader=...)``

View File

@ -1,6 +1,6 @@
"""Top-level package."""
__version__ = '4.11.0'
__version__ = '4.11.1'
"""Version number.
:type: str

File diff suppressed because it is too large Load Diff

View File

@ -541,16 +541,25 @@ def copy(object container):
:return: Declarative container's copying decorator.
:rtype: callable(:py:class:`DeclarativeContainer`)
"""
def _decorator(copied_container):
cdef dict memo = dict()
for name, provider in six.iteritems(copied_container.cls_providers):
def _get_providers_memo(from_providers, source_providers):
memo = dict()
for name, provider in from_providers.items():
try:
source_provider = getattr(container, name)
except AttributeError:
pass
source_provider = source_providers[name]
except KeyError:
...
else:
memo[id(source_provider)] = provider
if hasattr(provider, 'providers') and hasattr(source_provider, 'providers'):
sub_memo = _get_providers_memo(provider.providers, source_provider.providers)
memo.update(sub_memo)
return memo
def _decorator(copied_container):
memo = _get_providers_memo(copied_container.cls_providers, container.providers)
providers_copy = deepcopy(container.providers, memo)
for name, provider in six.iteritems(providers_copy):
setattr(copied_container, name, provider)

View File

@ -296,6 +296,26 @@ class DeclarativeContainerTests(unittest.TestCase):
self.assertEqual(_Container1.p13(), 11)
self.assertEqual(_Container2.p13(), 22)
def test_copy_with_replacing_subcontainer_providers(self):
# See: https://github.com/ets-labs/python-dependency-injector/issues/374
class X(containers.DeclarativeContainer):
foo = providers.Dependency(instance_of=str)
def build_x():
return X(foo='1')
class A(containers.DeclarativeContainer):
x = providers.DependenciesContainer(**X.providers)
y = x.foo
@containers.copy(A)
class B1(A):
x = providers.Container(build_x)
b1 = B1()
self.assertEqual(b1.y(), '1')
def test_containers_attribute(self):
class Container(containers.DeclarativeContainer):
class Container1(containers.DeclarativeContainer):