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

Dependency Injector 4.48.0+ returns _asyncio.Task instance instead of class instance

Open a-guzhin opened this issue 5 months ago • 1 comments

For Dependency Injector versions 4.48.0 and above, the app variable is initialized as an instance of _asyncio.Task:

from faststream import FastStream
from faststream.rabbit import RabbitBroker
from dependency_injector import containers, providers

class DI(containers.DeclarativeContainer):
    broker = providers.Resource(
        RabbitBroker,
    )

    app = providers.Resource(
        FastStream,
        broker=broker,
    )

di = DI()
di.init_resources()
app = di.app()

print(type(app))

However, if we change the broker provider type from Resource to Factory, the app variable correctly returns an instance of faststream.app.FastStream (as expected):

from faststream import FastStream
from faststream.rabbit import RabbitBroker
from dependency_injector import containers, providers

class DI(containers.DeclarativeContainer):
    broker = providers.Factory(
        RabbitBroker,
    )

    app = providers.Resource(
        FastStream,
        broker=broker,
    )

di = DI()
di.init_resources()
app = di.app()

print(type(app))

In Dependency Injector versions 4.47.1 and below, the app variable consistently returns an instance of faststream.app.FastStream, regardless of whether the broker is initialized as Resource or Factory.

Question: Why does Dependency Injector 4.48.0+ return an _asyncio.Task instance instead of faststream.app.FastStream when using providers.Resource?

Current Workaround: We've temporarily resolved the issue by downgrading to Dependency Injector version 4.47.1.

a-guzhin avatar Jul 10 '25 16:07 a-guzhin