-
Notifications
You must be signed in to change notification settings - Fork 18
Open
Description
Hi!
I was working on a project using rodi and I noticed that when a dependency that depends on another dependency has an __init__, it fails to resolve. Ok, I'm not good explaing things with human words, so as Linus said... talk is cheap, show me the code:
This works just fine:
class Foo:
def bar(self) -> str:
return 'bar'
class Baz:
foo: Foo
def call_foo(self) -> str:
return self.foo.bar()
container = Container()
container.register(Foo)
container.register(Baz)
baz = container.resolve(Baz)
# this one works
print(baz.call_foo()) # >>> bar But if we add a init, it gets angry
class Foo:
def bar(self) -> str:
return 'bar'
class Baz:
foo: Foo
+ def __init__(self) -> None:
+ pass
def call_foo(self) -> str:
return self.foo.bar()
container = Container()
container.register(Foo)
container.register(Baz)
baz = container.resolve(Baz)
# 💥
print(baz.call_foo())
# Traceback (most recent call last):
# File "whatever\test.py", blahblah
# return self.foo.bar()
# ^^^^^^^^
# AttributeError: 'Baz' object has no attribute 'foo'And this works too, but I love the idea of not having to use an __init__ if I don't need it (I know there's a whole activist movement out there claiming that constructor based injection is the new heaven. But I prefer property-based injection if that option is available, especially in python):
...
class Baz:
def __init__(self, foo: Foo) -> None:
self.foo = foo
...So, here's my question. Is this behaviour by design?
Metadata
Metadata
Assignees
Labels
No labels