New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Providing dependencies across more than one level of inheritance #350
Comments
Hmm... this work-around seems insufficient in more complex situations, especially in cases where there are "optional" dependencies, implemented as in #334 (comment). I don't really have a simple case to post -- but if the above doesn't have a "good" resolution and you need more information, I can see what I can do to boil one down. |
Hey @shaunc , that's good example, I'll take a look. |
Great -- thanks for the update! And thanks for picking off the easy ones! :) |
Confirming that's a bug. from dependency_injector import containers, providers
class C1(containers.DeclarativeContainer):
a1 = providers.Dependency(instance_of=str)
class C2(C1):
a2 = providers.Callable(C1.a1)
class C3(C2):
a3 = providers.Callable(C2.a1)
if __name__ == '__main__':
print('C1 providers', C1.providers.keys())
print('C2 providers', C2.providers.keys())
print('C3 providers', C3.providers.keys())
# Prints:
# C1 providers dict_keys(['a1'])
# C2 providers dict_keys(['a2', 'a1'])
# C3 providers dict_keys(['a3', 'a2']) <---- NO 'a1'
c3 = C3(a1='foo') # Raises AttributeError: 'DynamicContainer' object has no attribute 'a1' |
Working on a fix. |
Released a fix in from dependency_injector import containers, providers
class C1(containers.DeclarativeContainer):
a1 = providers.Dependency(instance_of=str)
class C2(C1):
a2 = providers.Callable(C1.a1)
class C3(C2):
a3 = providers.Callable(C2.a1)
if __name__ == '__main__':
print('C1 providers', C1.providers.keys())
print('C2 providers', C2.providers.keys())
print('C3 providers', C3.providers.keys())
# Prints:
# C1 providers dict_keys(['a1'])
# C2 providers dict_keys(['a2', 'a1'])
# C3 providers dict_keys(['a3', 'a2', 'a1'])
c3 = C3(a1='foo') # No error |
@shaunc , thanks for finding this one. It was kind of tricky and there were no tests for 2+ levels of inheritance. Many thanks! |
Closing the issue. Re-open if needed. |
I have a derived chain of containers (say,
C1 -> C2 -> C3
). I want to instantiateC3
with a dependency forC1
, 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:
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:(I tried fiddling with
@containers.copy()
as well, unsuccessfully).The text was updated successfully, but these errors were encountered: