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.

provide default for a dependencies container from another dependencies container

See original GitHub issue

Here is a case that is sort of a mix of other recipes you have provided: I would like to provide a default value for a dependencies container by taking it from another dependencies container:



from dependency_injector import containers, providers

class PV(containers.DeclarativeContainer):

    foo = providers.Object("foo")

PV_SPEC = dict(foo = providers.Dependency(instance_of = str))

class HasPV(containers.DeclarativeContainer):

    pv = providers.DependenciesContainer(**PV_SPEC)

HasPVSpec = dict(pv = providers.DependenciesContainer(**PV_SPEC))

class HasPV2(containers.DeclarativeContainer):

    has_pv = providers.DependenciesContainer(**HasPVSpec)
    pv = providers.DependenciesContainer(**PV_SPEC)
    pv.override(has_pv.pv)

This gives:

src/dependency_injector/providers.pyx in dependency_injector.providers.DependenciesContainer.override()

src/dependency_injector/providers.pyx in dependency_injector.providers.DependenciesContainer._override_providers()

src/dependency_injector/providers.pyx in dependency_injector.providers.Provider.override()

Error: Provider <dependency_injector.providers.Dependency(<class 'str'>) at 0x7fda702c96d0> could not be overridden with itself

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
rmk135commented, Jan 15, 2021

Seems #357 feature helps to resolve this one too:

from dependency_injector import containers, providers


class PV(containers.DeclarativeContainer):

    foo = providers.Object('foo')


class HasPV(containers.DeclarativeContainer):

    pv = providers.DependenciesContainer(**PV.dependencies)


class HasPV2(containers.DeclarativeContainer):

    has_pv = providers.DependenciesContainer(**HasPV.dependencies)
    pv = providers.DependenciesContainer(**PV.dependencies)
    pv.override(has_pv.pv)


if __name__ == '__main__':
    haspv2 = HasPV2(pv=PV(foo='bar'))
    print(haspv2.pv.foo())  # prints: bar

What do you think?

0reactions
rmk135commented, Jan 19, 2021

Good, thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Providing fallbacks / defaults for dependencies when using ...
1 Answer 1 · By constructing a new MyNewDefaultService() dependency in your consumer class, you are coupling to a concrete service class, in ......
Read more >
How To Manage Docker Dependencies With Ease
You can use it to create your application in a first “build” container and inject the results in a second container, without the...
Read more >
Control startup and shutdown order in Compose
Compose always starts and stops containers in dependency order, where dependencies are determined by depends_on , links , volumes_from , and network_mode: ...
Read more >
Container copying - Dependency Injector
Base container often keeps default dependencies while derived containers define overriding providers. Without @containers.copy() decorator, overridden providers ...
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 >

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