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

DynamicContainer injected at least two levels deep throws AttributeError

Open vlad-ghita opened this issue 11 months ago • 0 comments
trafficstars

Hey @rmk135

Thank you for a great project!

Your example works perfect for DeclarativeContainers.

However, I'm using a DynamicContainer at the lowest level.

This seems to cause confusion.

from dependency_injector import containers, providers


class C1(containers.DeclarativeContainer):
    deps = providers.DependenciesContainer()


class C2(containers.DeclarativeContainer):
    c1 = providers.DependenciesContainer()
    foo = c1.deps.foo


deps = containers.DynamicContainer()
deps.foo = providers.Object("foo")

c2 = C2(c1=C1(deps=deps))

assert "foo" == c2.foo()

Outputs:

Traceback (most recent call last):
  File "test2.py", line 8, in <module>
    class C2(containers.DeclarativeContainer):
  File "test2.py", line 10, in C2
    foo = c1.deps.foo
          ^^^^^^^^^^^
  File "src/dependency_injector/providers.pyx", line 847, in dependency_injector.providers.Dependency.__getattr__
AttributeError: Provider "Dependency" has no attribute "foo"

Could you, please, advise on how to achieve this?

I noticed a similar issue when nested level is a Configuration provider.

class C1(containers.DeclarativeContainer):
    # deps = providers.DependenciesContainer()
    config = providers.Configuration()

vlad-ghita avatar Dec 14 '24 15:12 vlad-ghita