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.

doc: override by derived container class

See original GitHub issue

I would like a derived container class to be able to provide a dependency for a base container. It would seem that this isn’t possible now:

class Base(containers.DeclarativeContainer):
  to_override = providers.Dependency(instance_of=str)

# method 1 -- override attribute
class Derived1(Base):

    other_dependency = providers.Dependency(instance_of=Foo)
    to_override = other_dependency.provided.some_string_attr

# method 2 -- use constructor
class Derived2(Base):

    def __init__(self, other_dependency: Foo, **kw):
        super_kw = kw.copy()
        if 'to_override' not in super_kw:
            super_kw['to_override'] = other_dependency.some_string_attr
        super().__init__(other_dependency=other_dependency, **super_kw)

    other_dependency = providers.Dependency(instance_of=Foo)

In either case I get dependency_injector.errors.Error: Dependency is not defined when I build a derived object and attempt to access to_override.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
rmk135commented, Feb 18, 2021

Added docs for the @containers.copy() decorator: https://python-dependency-injector.ets-labs.org/containers/copying.html. Closing the issue.

PS: @shaunc , appreciate your review if you would have some time. Please, drop me a line if there are any improvements on your mind.

1reaction
rmk135commented, Dec 13, 2020

Hey @shaunc . I’m away from computer right now, will take a look soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Derived class container from base class functions
I want to change the type of objects in a container located in a base class based on its derived type. (so the...
Read more >
Container (Java Platform SE 7 ) - Oracle Help Center
As of JDK version 1.1, replaced by doLayout() . Overrides: layout in class Component. isValidateRoot. public boolean isValidateRoot(). Indicates if ...
Read more >
Support override keyword on class methods #2000 - GitHub
An override keyword would be immensely useful in typescript. It would be an optional keyword on any method that overrides a super class...
Read more >
override specifier (since C++11) - cppreference.com
In a member function declaration or definition, override specifier ensures that the function is virtual and is overriding a virtual function ...
Read more >
collections.abc — Abstract Base Classes for Containers ...
This module provides abstract base classes that can be used to test ... The remaining mixin methods come from inheritance and can be...
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