question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Providing dependencies across more than one level of inheritance

See original GitHub issue

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).

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
rmk135commented, Jan 11, 2021

Hey @shaunc , that’s good example, I’ll take a look.

0reactions
rmk135commented, Jan 12, 2021

Closing the issue. Re-open if needed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Depedency version inheritance more than one level
I've given all dependent jars in dependencyManagement of Project A POM and version of those jars are maintained using property project.version.
Read more >
Handling Dependency Injection in Inherited Classes
In this post I have described a simple method for managing dependency injection in inherited classes, by creating a dependency aggregate class ......
Read more >
Inheritance Relationship - an overview | ScienceDirect Topics
We speak of single inheritance when the inheritance hierarchy is arranged in a tree, that is, if a class has only one direct...
Read more >
Maven: The Complete Reference - 3.5. Project Relationships
When Maven inherits dependencies, it will add dependencies of child projects to the dependencies defined in parent projects. You can use this feature...
Read more >
Hierarchical injectors - Angular
With hierarchical dependency injection, you can isolate sections of the application and give them their own private dependencies not shared with the rest...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found