python-dependency-injector
python-dependency-injector copied to clipboard
how to ensure that runtime changes to configuration are preserved?
Hi
I have the following scenario, and I am a bit blocked....
I have a container that I use to load all configurations Configuration, and to ensure that,
when changes are applied to any configuration, the same configuration is used, I nested
it inside another Container using a singleton provider :
import logging
import dependency_injector.containers as containers
import dependency_injector.providers as providers
class Configuration(containers.DeclarativeContainer):
config = providers.Configuration()
config.query_cache.enabled.from_env("CACHE_QUERIES", required=False,
default=False)
class Core(containers.DeclarativeContainer):
configuration = providers.ThreadSafeSingleton(Configuration)
Unfortunately, as experimented with this test case, any change to the configuration values is done, it's lost when calling Core and retrieving the configuration:
import pytest
from utils.config import Core
@pytest.fixture(scope="session")
def enable_caching():
config = Core().configuration().config
config.query_cache.enabled.from_value(True)
yield
config.query_cache.enabled.from_value(False)
def test_configuration(enable_caching):
config = Core().configuration().config
assert config.query_cache.enabled() == True # value is still False, like no from_value was applied...