Skip to content

DynamicContainer has no attribute "__self__" #579

Open
@EnrGer

Description

@EnrGer

Reading the Dependency Injector page I saw that it implements a DynamicContainer (https://python-dependency-injector.ets-labs.org/containers/dynamic.html) beyond the DeclarativeContainer I am currently using.

Right now I have this configuration:

# container.py
from dependency_injector import containers, providers


class BaseContainer(containers.DeclarativeContainer):
    lo = providers.Resource()
    me = providers.Resource()

And in my start_up file I defined:

# start_up.py 

# ...init providers.Resources/Singletons/etc...

container = Container()
container.lo = lo
container.me = me

what I am trying to achieve is to have a base container with the resources I always use, and in the start_up file define (if needed) other resources.

The problem is, when I'm using DynamicContainer as I intended it:

# container.py
from dependency_injector import containers, providers


class BaseContainer(containers.DynamicContainer):
    lo = providers.Resource()
    me = providers.Resource()
# start_up.py 

# ...init providers.Resources/Singletons/etc...

container = Container()
container.lo = lo
container.me = me
# new provider resource
container.si = si

I get this error when I'm passing this parameters (when i'm using DeclarativeContainer it works):

# cmd

...
def func(script: Script, provider: Provider, container=Provide[Container], lo=Provide[Container.lo]):
  File "...\dependency_injector\wiring.py", line 819, in __getitem__
    return cls(item)
  File "...\dependency_injector\wiring.py", line 830, in __init__
    provider = provider.__self__
AttributeError: type object 'Container' has no attribute '__self__'

Furthemore I tried to use directly the DeclarativeContainer but it seems there isn't a way to dynamically declare a new provider.

set_provider only works if the provider is already defined

Thank you for your help

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions