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

Cannot import name 'Container' from 'dependency_injector.containers' #386

Closed
ventaquil opened this issue Feb 2, 2021 · 5 comments
Closed
Assignees

Comments

@ventaquil
Copy link

ventaquil commented Feb 2, 2021

Hey!

I am trying to add type annotations to my Python code wherever I can. I created simple functions to init/shutdown resources in nested containers:

from dependency_injector.containers import Container
from dependency_injector.providers import Container as ContainerProvider


def init_resources(container: Container) -> NoReturn:
    container.init_resources()
    for provider in container.providers.values():
        if isinstance(provider, ContainerProvider):
            init_resources(provider)


def shutdown_resources(container: Container) -> NoReturn:
    container.shutdown_resources()
    for provider in container.providers.values():
        if isinstance(provider, ContainerProvider):
            shutdown_resources(provider)

I see that containers.pyi declares Container class but it seems that containers don't have Container parent at all.

I can prepare PR with abstract Container class.

@rmk135 rmk135 self-assigned this Feb 2, 2021
@rmk135
Copy link
Member

rmk135 commented Feb 2, 2021

Hi @ventaquil ,

I see that containers.pyi declares Container class but it seems that containers don't have Container parent at all.

Yep, Container is just a typing stub, not a real class. I think creating just an empty class should be enough to keep using the stub for typing and not failing in runtime.

If you could make a pull request, it would be awesome. Use "develop" as a target branch.

@rmk135
Copy link
Member

rmk135 commented Feb 2, 2021

Btw, starting from version 4.14.0 you don't need to have these recursive functions. When you call .init_resources() on root container, it will initialize all nested resources. The same for shutdown. If you have other recursive functions, see new feature: https://python-dependency-injector.ets-labs.org/containers/traversal.html

@rmk135
Copy link
Member

rmk135 commented Feb 3, 2021

Fixed in 4.16.0. Thanks for reporting the problem. Closing the issue, re-open if needed.

@rmk135 rmk135 closed this as completed Feb 3, 2021
@ventaquil
Copy link
Author

I haven't time yet to do it on my own 😢

Thank you for fix and recommendations about .init_resources().

@rmk135
Copy link
Member

rmk135 commented Feb 4, 2021

I haven't time yet to do it on my own 😢

No worries! Thanks for finding time to report the issue. It helps to improve Dependency Injector.

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

No branches or pull requests

2 participants