Skip to content

Providing dependencies across more than one level of inheritance #350

@shaunc

Description

@shaunc

I have a derived chain of containers (say, C1 -> C2 -> C3). I want to instantiate C3 with a dependency for C1, which is then used (directly and indirectly) in the derived containers.

In fact, I have found a way to do it, but it is ugly:

class C1(containers.DeclarativeContainer):
    a1 = providers.Dependency(instance_of=str)


class C2(C1):
    a1_ = C1.a1
    a1 = providers.Dependency(instance_of=str)
    a1_.override(a1)
    a2 = providers.Callable(C1.a1)


class C3(C2):
    a3 = providers.Callable(C2.a1)

c3 = C3(a1="foo")
print("c3", c3.a3())  # prints "foo"

I can't seem to get it to work without the intermediate redefinition of a1 ... is this a bug? E.g. if I don't have that and try to instantiate C3(a1="foo") I get an error:

AttributeError: 'DynamicContainer' object has no attribute 'a1'

(I tried fiddling with @containers.copy() as well, unsuccessfully).

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions