I'm trying to use dependency injector in Python provided by this package: dependency injector. I'm using python3.6.8 on a linux machine.
This is my tree:
|-main.py
|-dummy_objects
| |-dummy.py
| |-init.py
|-containers
| |-dependencies.py
| |-init.py
And the code:
dummy.py
class Dummy:
def __init__(self, name: str):
self._name = name
def __repr__(self):
return f'Name: {self._name}'
def print_repr(self):
print(self.__repr__())
dependencies.py
import sys
from dependency_injector import containers, providers
from dummy_objects.dummy import Dummy
class Container(containers.DeclarativeContainer):
config = providers.Configuration(strict=True)
dummy1 = providers.Factory(Dummy, config.name1)
def get_container():
container = Container()
container.config.name1.from_value('Dummy 1')
container.wire(modules=[sys.modules[__name__],
# sys.modules['containers'],
sys.modules['dummy_objects'],
sys.modules['dummy_objects.dummy'],
])
return container
main.py
from dummy_objects.dummy import Dummy
from containers.dependencies import get_container
from dependency_injector.wiring import inject, Provide
container = get_container()
@inject
def main(dummy1: Dummy = Provide[container.dummy1]) -> None:
dummy2 = Dummy('Test object 1')
print(dummy1)
print(dummy2)
if __name__ == '__main__':
container = get_container()
main()
The result is:
> python3 main.py
<dependency_injector.wiring.Provide object at 0x7f5c068fa278>
Name: Test object 1
The expected result should be:
python3 main.py
Name: Dummy 1
Name: Test object 1
What am I doing wrong? or is it just a bug? I already opened an issue on the git here: git repo
I do not know if it is directly related but after I downgraded dependency-injector from 4.39.1 to 4.38.0 it started working. Hope it helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With