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.

Can't override base class dependencies container with dependency built by callable

See original GitHub issue

Here is another tricky one I stumbled on –


from dependency_injector import providers, containers

class X(containers.DeclarativeContainer):
    foo = providers.Dependency(instance_of=str)

def build_x():
  return X(foo="1")

class A(containers.DeclarativeContainer):

  x = providers.DependenciesContainer(**X.providers)
  y = x.foo

@containers.copy(A)
class B1(A):
  some_x = providers.Callable(build_x)
  x = some_x.provided

b1 = B1()
print(b1.y())  # fails with "Dependency is not defined"

I also can’t get it to work by repeating x definition and overriding:

@containers.copy(A)
class B2(A):
  some_x = providers.Callable(build_x)
  x = providers.DependenciesContainer(**X.providers)
  x.override(some_x.provided)

Gives TypeError: 'builtin_function_or_method' object is not iterable on module load.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
rmk135commented, Jan 27, 2021

Fixed in 4.11.1. It’s been publishing on PyPI right now and will be ready as soon as https://github.com/ets-labs/python-dependency-injector/actions/runs/515305692 is done. Btw, it’s a new CD pipeline on Github Actions.

1reaction
rmk135commented, Jan 27, 2021

Hey @shaunc ,

Here is a way to make it work:

from dependency_injector import providers, containers

class X(containers.DeclarativeContainer):
  foo = providers.Dependency(instance_of=str)

def build_x():
  return X(foo="1")

class A(containers.DeclarativeContainer):

  x = providers.DependenciesContainer(**X.providers)
  y = x.foo

@containers.copy(A)
class B2(A):
  some_x = providers.Container(build_x)  # <-- Replace Callable with Container
  x = providers.DependenciesContainer(**X.providers)
  x.override(some_x)  # <-- Remove .provided

b1 = B2()
print(b1.y())  # fails with "Dependency is not defined"

PS: My apologies it took so long to answer. Was overwhelmed with other stuff.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Implicitly injecting dependency in Base class while derived ...
No, unity is unable to do so. Actually, there's not a single container who can do so. A constructor is there to instantiate...
Read more >
Classes as Dependencies - FastAPI
The key factor is that a dependency should be a "callable". A "callable" in Python is anything that Python can "call" like a...
Read more >
Issue #336 · ets-labs/python-dependency-injector - GitHub
I am trying to create a container that optionally takes a dependency, and otherwise provides a value derived from another provider.
Read more >
Handling Dependency Injection in Inherited Classes
Conclusion. In this post I have described a simple method for managing dependency injection in inherited classes, by creating a dependency ...
Read more >
Service Container - The PHP Framework For Web Artisans
Dependency injection is a fancy phrase that essentially means this: class dependencies are "injected" into the class via the constructor or, in some...
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