Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python package dependency_injector wiring not working as expected

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

like image 622
user8627671 Avatar asked Aug 20 '26 02:08

user8627671


1 Answers

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!

like image 126
Mitchell Mullen Avatar answered Aug 23 '26 19:08

Mitchell Mullen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!