Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate configuration values on multiple containers #428

Closed
dcendents opened this issue Mar 19, 2021 · 3 comments
Closed

Propagate configuration values on multiple containers #428

dcendents opened this issue Mar 19, 2021 · 3 comments
Assignees
Labels

Comments

@dcendents
Copy link

Hi,

I'd like to use mutiple containers like in the example: https://python-dependency-injector.ets-labs.org/examples/application-multiple-containers.html

But I have the following problem, my factories use the first configuration value and ignore the new values.
If I use a single container it works fine.

Here is an example that re-create the problem:

from dependency_injector import containers, providers


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)


if __name__ == '__main__':
    container = Core()

    container.config.set("greeting", "Hello World")
    assert container.greetings() == "Hello World"

    container.config.set("greeting", "Hello Bob")
    assert container.greetings() == "Hello Bob"

    container = Application()

    container.config.set("greeting", "Hello World")
    assert container.greetings() == "Hello World"
    assert container.core.greetings() == "Hello World"

    container.config.set("greeting", "Hello Bob")
    assert container.greetings() == "Hello Bob"
    # Last one will fail, value is still "Hello World"
    assert container.core.greetings() == "Hello Bob"

I'd appreciate if you could tell me why the first value set can be seen by the Core container but subsequent updates are ignored

Thanks

@rmk135 rmk135 self-assigned this Mar 20, 2021
@rmk135
Copy link
Member

rmk135 commented Mar 20, 2021

Hi @dcendents ,

Cool finding. That's definitely a bug. Configuration provider has a cache. It uses cache to avoid traveling for the value to a root provider. In that case cache root provider does not reset cache for a child config provider in a child container.

The workaround is to manually call container.core.config.reset_cache(). I'll also publish a fix on PyPI. Thanks for reporting the issue.

@rmk135 rmk135 added the bug label Mar 20, 2021
@rmk135
Copy link
Member

rmk135 commented Mar 21, 2021

@dcendents I've published a fix in 4.31.0. Thanks again for reporting the issue.

@rmk135 rmk135 closed this as completed Mar 21, 2021
@dcendents
Copy link
Author

Thanks @rmk135 it was quick and indeed 4.31.0 fixes the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants