python-dependency-injector icon indicating copy to clipboard operation
python-dependency-injector copied to clipboard

DynamicContainer has no attribute "__self__"

Open EnrGer opened this issue 3 years ago • 0 comments

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

EnrGer avatar Apr 08 '22 12:04 EnrGer