python-dependency-injector icon indicating copy to clipboard operation
python-dependency-injector copied to clipboard

Wiring depenencies using "app.module" does not work the same as sys.module

Open ben-youngblut opened this issue 2 years ago • 0 comments
trafficstars

So I am pulling my hair out on this one, I have a different application where I use wire(packages=[]) that works which just makes this more frustrating.

Here is my test case...

import uuid
import sys

from dependency_injector.wiring import inject, Provide
from dependency_injector import containers, providers


class Service:
    def __init__(self):
        self.uuid = uuid.uuid4()


class Container(containers.DeclarativeContainer):
    service = providers.Factory(Service)


@inject
def get_service_uuid(service: Service = Provide["service"]) -> None:
    print(service.uuid)


def setup():
    container = Container()
    container.wire(modules=[sys.modules[__name__]])  # Works
    # container.wire(modules=["__main__"])  # Works
    # container.wire(modules=["namespace.app.test"])  # Does not work
    # container.wire(packages=["namespace.app"])  # Does not work


if __name__ == '__main__':
    setup()
    get_service_uuid()

My file in called test in namespace.app.test where namespace is in the python path but does not have an init.py but namespace.app does have an init.py

I am running the latest version 4.41.0.

The wiring works in all 4 examples without errors... but I get the

AttributeError: 'Provide' object has no attribute 'uuid'

error for the last 2 versions.

I have stepped through wiring.py and can see that the function is being recorded in from dependency_injector.wiring import _patched_registry

I am really struggling to figure out what is wrong... (my main code has these files all split out into a providers.py and things like that and I get the same error...)

Any help would be greatly appreciated.

ben-youngblut avatar Aug 31 '23 21:08 ben-youngblut