Fix configuration cache reset (#430)

* Implement fix

* Improve providers copying

* Add tests and implement cache reset for configuration option

* Update changelog

* Add link to the issue
This commit is contained in:
Roman Mogylatov 2021-03-20 21:41:39 -04:00 committed by GitHub
parent f961ff536a
commit 41e698f633
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 13147 additions and 11416 deletions

View File

@ -14,6 +14,9 @@ Development version
- Improve typing in wiring module. - Improve typing in wiring module.
- Fix wiring module loader uninstallation issue. - Fix wiring module loader uninstallation issue.
- Fix provided instance providers error handing in asynchronous mode. - Fix provided instance providers error handing in asynchronous mode.
- Fix overridden configuration option cache resetting.
See issue: `#428 <https://github.com/ets-labs/python-dependency-injector/issues/428>`_.
Thanks to `@dcendents <https://github.com/dcendents>`_ for reporting the issue.
4.30.0 4.30.0
------ ------

File diff suppressed because it is too large Load Diff

View File

@ -88,14 +88,14 @@ class DynamicContainer(Container):
copied = self.__class__() copied = self.__class__()
memo[id(self)] = copied memo[id(self)] = copied
copied.provider_type = providers.Provider
copied.overridden = providers.deepcopy(self.overridden, memo)
copied.declarative_parent = self.declarative_parent
copied.__self__ = providers.deepcopy(self.__self__, memo) copied.__self__ = providers.deepcopy(self.__self__, memo)
for name in copied.__self__.alt_names: for name in copied.__self__.alt_names:
copied.set_provider(name, copied.__self__) copied.set_provider(name, copied.__self__)
copied.provider_type = providers.Provider
copied.overridden = providers.deepcopy(self.overridden, memo)
copied.declarative_parent = self.declarative_parent
for name, provider in providers.deepcopy(self.providers, memo).items(): for name, provider in providers.deepcopy(self.providers, memo).items():
copied.set_provider(name, provider) copied.set_provider(name, provider)

File diff suppressed because it is too large Load Diff

View File

@ -14,6 +14,7 @@ cimport cython
cdef class Provider(object): cdef class Provider(object):
cdef tuple __overridden cdef tuple __overridden
cdef Provider __last_overriding cdef Provider __last_overriding
cdef tuple __overrides
cdef int __async_mode cdef int __async_mode
cpdef object _provide(self, tuple args, dict kwargs) cpdef object _provide(self, tuple args, dict kwargs)

View File

@ -61,6 +61,10 @@ class Provider(Generic[T]):
def override(self, provider: Union[Provider, Any]) -> OverridingContext[P]: ... def override(self, provider: Union[Provider, Any]) -> OverridingContext[P]: ...
def reset_last_overriding(self) -> None: ... def reset_last_overriding(self) -> None: ...
def reset_override(self) -> None: ... def reset_override(self) -> None: ...
@property
def overrides(self) -> Tuple[Provider]: ...
def register_overrides(self, provider: Union[Provider, Any]) -> None: ...
def unregister_overrides(self, provider: Union[Provider, Any]) -> None: ...
def delegate(self) -> Provider: ... def delegate(self) -> Provider: ...
@property @property
def provider(self) -> Provider: ... def provider(self) -> Provider: ...

View File

@ -176,6 +176,7 @@ cdef class Provider(object):
"""Initializer.""" """Initializer."""
self.__overridden = tuple() self.__overridden = tuple()
self.__last_overriding = None self.__last_overriding = None
self.__overrides = tuple()
self.__async_mode = ASYNC_MODE_UNDEFINED self.__async_mode = ASYNC_MODE_UNDEFINED
super(Provider, self).__init__() super(Provider, self).__init__()
@ -208,10 +209,8 @@ cdef class Provider(object):
if copied is not None: if copied is not None:
return copied return copied
copied = self.__class__() copied = _memorized_duplicate(self, memo)
self._copy_overridings(copied, memo) self._copy_overridings(copied, memo)
return copied return copied
@classmethod @classmethod
@ -267,6 +266,7 @@ cdef class Provider(object):
with self.overriding_lock: with self.overriding_lock:
self.__overridden += (provider,) self.__overridden += (provider,)
self.__last_overriding = provider self.__last_overriding = provider
provider.register_overrides(self)
return OverridingContext(self, provider) return OverridingContext(self, provider)
@ -282,6 +282,8 @@ cdef class Provider(object):
if len(self.__overridden) == 0: if len(self.__overridden) == 0:
raise Error('Provider {0} is not overridden'.format(str(self))) raise Error('Provider {0} is not overridden'.format(str(self)))
self.__last_overriding.unregister_overrides(self)
self.__overridden = self.__overridden[:-1] self.__overridden = self.__overridden[:-1]
try: try:
self.__last_overriding = self.__overridden[-1] self.__last_overriding = self.__overridden[-1]
@ -294,9 +296,27 @@ cdef class Provider(object):
:rtype: None :rtype: None
""" """
with self.overriding_lock: with self.overriding_lock:
for provider in self.__overridden:
provider.unregister_overrides(self)
self.__overridden = tuple() self.__overridden = tuple()
self.__last_overriding = None self.__last_overriding = None
@property
def overrides(self):
"""Return providers that are overridden by the current provider."""
return self.__overrides
def register_overrides(self, provider):
"""Register provider that overrides current provider."""
self.__overrides = tuple(set(self.__overrides + (provider,)))
def unregister_overrides(self, provider):
"""Unregister provider that overrides current provider."""
overrides = set(self.__overrides)
if provider in overrides:
overrides.remove(provider)
self.__overrides = tuple(overrides)
def async_(self, *args, **kwargs): def async_(self, *args, **kwargs):
"""Return provided object asynchronously. """Return provided object asynchronously.
@ -388,6 +408,7 @@ cdef class Provider(object):
"""Copy provider overridings to a newly copied provider.""" """Copy provider overridings to a newly copied provider."""
copied.__overridden = deepcopy(self.__overridden, memo) copied.__overridden = deepcopy(self.__overridden, memo)
copied.__last_overriding = deepcopy(self.__last_overriding, memo) copied.__last_overriding = deepcopy(self.__last_overriding, memo)
copied.__overrides = deepcopy(self.__overrides, memo)
cdef class Object(Provider): cdef class Object(Provider):
@ -479,14 +500,10 @@ cdef class Self(Provider):
if copied is not None: if copied is not None:
return copied return copied
copied = self.__class__() copied = _memorized_duplicate(self, memo)
memo[id(self)] = copied
copied.set_container(deepcopy(self.__container, memo)) copied.set_container(deepcopy(self.__container, memo))
copied.set_alt_names(self.__alt_names) copied.set_alt_names(self.__alt_names)
self._copy_overridings(copied, memo) self._copy_overridings(copied, memo)
return copied return copied
def __str__(self): def __str__(self):
@ -539,13 +556,8 @@ cdef class Delegate(Provider):
if copied is not None: if copied is not None:
return copied return copied
provides = self.provides
if provides:
provides = deepcopy(self.provides, memo)
copied = _memorized_duplicate(self, memo) copied = _memorized_duplicate(self, memo)
copied.set_provides(provides) copied.set_provides(_copy_if_provider(self.provides, memo))
self._copy_overridings(copied, memo) self._copy_overridings(copied, memo)
return copied return copied
@ -887,12 +899,9 @@ cdef class DependenciesContainer(Object):
if copied is not None: if copied is not None:
return copied return copied
copied = self.__class__() copied = <DependenciesContainer> _memorized_duplicate(self, memo)
memo[id(self)] = copied
copied.__provides = deepcopy(self.__provides, memo) copied.__provides = deepcopy(self.__provides, memo)
copied.__providers = deepcopy(self.__providers, memo) copied.__providers = deepcopy(self.__providers, memo)
self._copy_parent(copied, memo) self._copy_parent(copied, memo)
self._copy_overridings(copied, memo) self._copy_overridings(copied, memo)
@ -1059,17 +1068,11 @@ cdef class Callable(Provider):
if copied is not None: if copied is not None:
return copied return copied
provides = self.provides
if isinstance(provides, Provider):
provides = deepcopy(provides, memo)
copied = _memorized_duplicate(self, memo) copied = _memorized_duplicate(self, memo)
copied.set_provides(provides) copied.set_provides(_copy_if_provider(self.provides, memo))
copied.set_args(*deepcopy(self.args, memo)) copied.set_args(*deepcopy(self.args, memo))
copied.set_kwargs(**deepcopy(self.kwargs, memo)) copied.set_kwargs(**deepcopy(self.kwargs, memo))
self._copy_overridings(copied, memo) self._copy_overridings(copied, memo)
return copied return copied
def __str__(self): def __str__(self):
@ -1380,7 +1383,7 @@ cdef class ConfigurationOption(Provider):
:py:class:`Configuration` provider. :py:class:`Configuration` provider.
""" """
def __init__(self, name, Configuration root, required=False): def __init__(self, name=None, Configuration root=None, required=False):
self.__name = name self.__name = name
self.__root = root self.__root = root
self.__children = {} self.__children = {}
@ -1395,12 +1398,12 @@ cdef class ConfigurationOption(Provider):
if copied is not None: if copied is not None:
return copied return copied
copied_name = deepcopy(self.__name, memo) copied = <ConfigurationOption> _memorized_duplicate(self, memo)
copied_root = deepcopy(self.__root, memo) copied.__name = deepcopy(self.__name, memo)
copied.__root = deepcopy(self.__root, memo)
copied = self.__class__(copied_name, copied_root, self.__required)
copied.__children = deepcopy(self.__children, memo) copied.__children = deepcopy(self.__children, memo)
copied.__required = self.__required
self._copy_overridings(copied, memo)
return copied return copied
def __enter__(self): def __enter__(self):
@ -1486,8 +1489,13 @@ cdef class ConfigurationOption(Provider):
def reset_cache(self): def reset_cache(self):
self.__cache = UNDEFINED self.__cache = UNDEFINED
for child in self.__children.values():
child.reset_cache() for provider in self.__children.values():
provider.reset_cache()
for provider in self.overrides:
if isinstance(provider, (Configuration, ConfigurationOption)):
provider.reset_cache()
def update(self, value): def update(self, value):
"""Set configuration options. """Set configuration options.
@ -1893,8 +1901,12 @@ cdef class Configuration(Object):
:rtype: None :rtype: None
""" """
for child in self.__children.values(): for provider in self.__children.values():
child.reset_cache() provider.reset_cache()
for provider in self.overrides:
if isinstance(provider, (Configuration, ConfigurationOption)):
provider.reset_cache()
def update(self, value): def update(self, value):
"""Set configuration options. """Set configuration options.
@ -2149,18 +2161,12 @@ cdef class Factory(Provider):
if copied is not None: if copied is not None:
return copied return copied
provides = self.provides
if isinstance(provides, Provider):
provides = deepcopy(provides, memo)
copied = _memorized_duplicate(self, memo) copied = _memorized_duplicate(self, memo)
copied.set_provides(provides) copied.set_provides(_copy_if_provider(self.provides, memo))
copied.set_args(*deepcopy(self.args, memo)) copied.set_args(*deepcopy(self.args, memo))
copied.set_kwargs(**deepcopy(self.kwargs, memo)) copied.set_kwargs(**deepcopy(self.kwargs, memo))
copied.set_attributes(**deepcopy(self.attributes, memo)) copied.set_attributes(**deepcopy(self.attributes, memo))
self._copy_overridings(copied, memo) self._copy_overridings(copied, memo)
return copied return copied
def __str__(self): def __str__(self):
@ -2524,18 +2530,12 @@ cdef class BaseSingleton(Provider):
if copied is not None: if copied is not None:
return copied return copied
provides = self.provides
if isinstance(provides, Provider):
provides = deepcopy(provides, memo)
copied = _memorized_duplicate(self, memo) copied = _memorized_duplicate(self, memo)
copied.set_provides(provides) copied.set_provides(_copy_if_provider(self.provides, memo))
copied.set_args(*deepcopy(self.args, memo)) copied.set_args(*deepcopy(self.args, memo))
copied.set_kwargs(**deepcopy(self.kwargs, memo)) copied.set_kwargs(**deepcopy(self.kwargs, memo))
copied.set_attributes(**deepcopy(self.attributes, memo)) copied.set_attributes(**deepcopy(self.attributes, memo))
self._copy_overridings(copied, memo) self._copy_overridings(copied, memo)
return copied return copied
@property @property
@ -3063,9 +3063,9 @@ cdef class List(Provider):
if copied is not None: if copied is not None:
return copied return copied
copied = self.__class__(*deepcopy(self.args, memo)) copied = _memorized_duplicate(self, memo)
copied.set_args(*deepcopy(self.args, memo))
self._copy_overridings(copied, memo) self._copy_overridings(copied, memo)
return copied return copied
def __str__(self): def __str__(self):
@ -3171,9 +3171,9 @@ cdef class Dict(Provider):
if copied is not None: if copied is not None:
return copied return copied
copied = self.__class__(deepcopy(self.kwargs, memo)) copied = _memorized_duplicate(self, memo)
copied.set_kwargs(**deepcopy(self.kwargs, memo))
self._copy_overridings(copied, memo) self._copy_overridings(copied, memo)
return copied return copied
def __str__(self): def __str__(self):
@ -3595,12 +3595,12 @@ cdef class Container(Provider):
Provider is experimental. Its interface may change. Provider is experimental. Its interface may change.
""" """
def __init__(self, container_cls, container=None, **overriding_providers): def __init__(self, container_cls=None, container=None, **overriding_providers):
"""Initialize provider.""" """Initialize provider."""
self.__container_cls = container_cls self.__container_cls = container_cls
self.__overriding_providers = overriding_providers self.__overriding_providers = overriding_providers
if container is None: if container is None and container_cls:
container = container_cls() container = container_cls()
container.assign_parent(self) container.assign_parent(self)
self.__container = container self.__container = container
@ -3620,15 +3620,12 @@ cdef class Container(Provider):
if copied is not None: if copied is not None:
return copied return copied
copied = self.__class__(self.__container_cls, UNDEFINED) copied = <Container> _memorized_duplicate(self, memo)
memo[id(self)] = copied copied.__container_cls = self.__container_cls
copied.__container = deepcopy(self.__container, memo) copied.__container = deepcopy(self.__container, memo)
copied.__overriding_providers = deepcopy(self.__overriding_providers, memo) copied.__overriding_providers = deepcopy(self.__overriding_providers, memo)
copied.apply_overridings()
self._copy_parent(copied, memo) self._copy_parent(copied, memo)
self._copy_overridings(copied, memo)
return copied return copied
def __getattr__(self, name): def __getattr__(self, name):
@ -4250,13 +4247,13 @@ cdef class Injection(object):
cdef class PositionalInjection(Injection): cdef class PositionalInjection(Injection):
"""Positional injection class.""" """Positional injection class."""
def __init__(self, value): def __init__(self, value=None):
"""Initializer.""" """Initializer."""
self.__value = value self.__value = None
self.__is_provider = <int>is_provider(value) self.__is_provider = 0
self.__is_delegated = <int>is_delegated(value) self.__is_delegated = 0
self.__call = <int>(self.__is_provider == 1 and self.__call = 0
self.__is_delegated == 0) self.set(value)
super(PositionalInjection, self).__init__() super(PositionalInjection, self).__init__()
def __deepcopy__(self, memo): def __deepcopy__(self, memo):
@ -4264,7 +4261,9 @@ cdef class PositionalInjection(Injection):
copied = memo.get(id(self)) copied = memo.get(id(self))
if copied is not None: if copied is not None:
return copied return copied
return self.__class__(deepcopy(self.__value, memo)) copied = _memorized_duplicate(self, memo)
copied.set(_copy_if_provider(self.__value, memo))
return copied
def get_value(self): def get_value(self):
"""Return injection value.""" """Return injection value."""
@ -4274,18 +4273,28 @@ cdef class PositionalInjection(Injection):
"""Return original value.""" """Return original value."""
return self.__value return self.__value
def set(self, value):
"""Set injection."""
self.__value = value
self.__is_provider = <int>is_provider(value)
self.__is_delegated = <int>is_delegated(value)
self.__call = <int>(self.__is_provider == 1 and self.__is_delegated == 0)
cdef class NamedInjection(Injection): cdef class NamedInjection(Injection):
"""Keyword injection class.""" """Keyword injection class."""
def __init__(self, name, value): def __init__(self, name=None, value=None):
"""Initializer.""" """Initializer."""
self.__name = name self.__name = name
self.__value = value self.set_name(name)
self.__is_provider = <int>is_provider(value)
self.__is_delegated = <int>is_delegated(value) self.__value = None
self.__call = <int>(self.__is_provider == 1 and self.__is_provider = 0
self.__is_delegated == 0) self.__is_delegated = 0
self.__call = 0
self.set(value)
super(NamedInjection, self).__init__() super(NamedInjection, self).__init__()
def __deepcopy__(self, memo): def __deepcopy__(self, memo):
@ -4293,13 +4302,19 @@ cdef class NamedInjection(Injection):
copied = memo.get(id(self)) copied = memo.get(id(self))
if copied is not None: if copied is not None:
return copied return copied
return self.__class__(deepcopy(self.__name, memo), copied = _memorized_duplicate(self, memo)
deepcopy(self.__value, memo)) copied.set_name(self.get_name())
copied.set(_copy_if_provider(self.__value, memo))
return copied
def get_name(self): def get_name(self):
"""Return injection value.""" """Return injection name."""
return __get_name(self) return __get_name(self)
def set_name(self, name):
"""Set injection name."""
self.__name = name
def get_value(self): def get_value(self):
"""Return injection value.""" """Return injection value."""
return __get_value(self) return __get_value(self)
@ -4308,6 +4323,13 @@ cdef class NamedInjection(Injection):
"""Return original value.""" """Return original value."""
return self.__value return self.__value
def set(self, value):
"""Set injection."""
self.__value = value
self.__is_provider = <int>is_provider(value)
self.__is_delegated = <int>is_delegated(value)
self.__call = <int>(self.__is_provider == 1 and self.__is_delegated == 0)
@cython.boundscheck(False) @cython.boundscheck(False)
@cython.wraparound(False) @cython.wraparound(False)

View File

@ -416,6 +416,60 @@ class ConfigLinkingTests(unittest.TestCase):
self.assertEqual(services.config.value(), 'services2') self.assertEqual(services.config.value(), 'services2')
self.assertEqual(services.value_getter(), 'services2') self.assertEqual(services.value_getter(), 'services2')
def test_reset_overriding_cache(self):
# See: https://github.com/ets-labs/python-dependency-injector/issues/428
class Core(containers.DeclarativeContainer):
config = providers.Configuration()
greetings = providers.Factory(str, config.greeting)
class Application(containers.DeclarativeContainer):
config = providers.Configuration()
core = providers.Container(
Core,
config=config,
)
greetings = providers.Factory(str, config.greeting)
container = Application()
container.config.set('greeting', 'Hello World')
self.assertEqual(container.greetings(), 'Hello World')
self.assertEqual(container.core.greetings(), 'Hello World')
container.config.set('greeting', 'Hello Bob')
self.assertEqual(container.greetings(), 'Hello Bob')
self.assertEqual(container.core.greetings(), 'Hello Bob')
def test_reset_overriding_cache_for_option(self):
# See: https://github.com/ets-labs/python-dependency-injector/issues/428
class Core(containers.DeclarativeContainer):
config = providers.Configuration()
greetings = providers.Factory(str, config.greeting)
class Application(containers.DeclarativeContainer):
config = providers.Configuration()
core = providers.Container(
Core,
config=config.option,
)
greetings = providers.Factory(str, config.option.greeting)
container = Application()
container.config.set('option.greeting', 'Hello World')
self.assertEqual(container.greetings(), 'Hello World')
self.assertEqual(container.core.greetings(), 'Hello World')
container.config.set('option.greeting', 'Hello Bob')
self.assertEqual(container.greetings(), 'Hello Bob')
self.assertEqual(container.core.greetings(), 'Hello Bob')
class ConfigFromIniTests(unittest.TestCase): class ConfigFromIniTests(unittest.TestCase):