Merge branch 'release/4.8.2' into master

This commit is contained in:
Roman Mogylatov 2021-01-13 17:08:36 -05:00
commit 1cbd3a0215
7 changed files with 5951 additions and 5606 deletions

View File

@ -7,6 +7,13 @@ that were made in every particular version.
From version 0.7.6 *Dependency Injector* framework strictly
follows `Semantic versioning`_
4.8.2
-----
- Fix ``Container`` provider to apply context overridings on root container initialization.
See issue `#354 <https://github.com/ets-labs/python-dependency-injector/issues/354>`_.
Many thanks to `Shaun Cutts <https://github.com/shaunc>`_ for submitting the issue.
- Hotfix for version ``4.8.0``: fix side effect in ``Container`` provider overriding.
4.8.1
-----
- Fix declarative container multi-level inheritance issue.

View File

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

File diff suppressed because it is too large Load Diff

View File

@ -15,6 +15,7 @@ from .providers cimport (
Provider,
Object,
Resource,
Container as ContainerProvider,
deepcopy,
)
@ -252,6 +253,13 @@ class DynamicContainer(object):
if futures:
return asyncio.gather(*futures)
def apply_container_providers_overridings(self):
"""Apply container providers' overridings."""
for provider in self.providers.values():
if not isinstance(provider, ContainerProvider):
continue
provider.apply_overridings()
class DeclarativeContainerMetaClass(type):
"""Declarative inversion of control container meta class."""
@ -407,6 +415,7 @@ class DeclarativeContainer(object):
container.declarative_parent = cls
container.set_providers(**deepcopy(cls.providers))
container.override_providers(**overriding_providers)
container.apply_container_providers_overridings()
return container
@classmethod

File diff suppressed because it is too large Load Diff

View File

@ -765,10 +765,7 @@ cdef class DependenciesContainer(Object):
cpdef object _override_providers(self, object container):
"""Override providers with providers from provided container."""
for name, dependency_provider in container.providers.items():
provider = self.__providers.get(name)
if not provider:
continue
provider = getattr(self, name)
if provider.last_overriding is dependency_provider:
continue
@ -3041,7 +3038,6 @@ cdef class Container(Provider):
if container is None:
container = container_cls()
container.override_providers(**overriding_providers)
self.__container = container
super(Container, self).__init__()
@ -3082,9 +3078,16 @@ cdef class Container(Provider):
if not hasattr(provider, 'providers'):
raise Error('Container provider {0} can be overridden only by providers container'.format(self))
self.__container.override_providers = provider.providers
self.__container.override_providers(**provider.providers)
super().override(provider)
def apply_overridings(self):
"""Apply container overriding.
This method should not be called directly. It is called on
declarative container initialization."""
self.__container.override_providers(**self.__overriding_providers)
cpdef object _provide(self, tuple args, dict kwargs):
"""Return single instance."""
return self.__container

View File

@ -76,3 +76,21 @@ class ContainerTests(unittest.TestCase):
with self.assertRaises(errors.Error):
provider.override(providers.Object('foo'))
def test_lazy_overriding(self):
class D(containers.DeclarativeContainer):
foo = providers.Object("foo")
class A(containers.DeclarativeContainer):
d = providers.DependenciesContainer()
bar = providers.Callable(lambda f: f + "++", d.foo.provided)
class B(containers.DeclarativeContainer):
d = providers.DependenciesContainer()
a = providers.Container(A, d=d)
b = B(d=D())
result = b.a().bar()
self.assertEqual(result, 'foo++')
# See: https://github.com/ets-labs/python-dependency-injector/issues/354