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

Injecting an async resource into an async generator provides future instead of resource

Open tkuenzle opened this issue 2 years ago • 5 comments

First of all, thanks a lot for this library - it's really great work you are doing here!

We are relying on this library for a FastAPI project and we noticed that when we try to inject an async resource into an async generator, somehow the injected resource is still wrapped in a future.

We would expect the following minimal example to run without exception, but what we see is

AssertionError: resource is of type <class '_asyncio.Future'>

Of course we can easily work around that by doing another await to get the actual resource, but it would of course be great if this could be fixed.

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

class Resource:
    pass

async def async_resource():
    yield Resource()

class Container(containers.DeclarativeContainer):
    resource = providers.Resource(async_resource)

@inject
async def async_generator(resource = Provide[Container.resource]):
    yield resource

async def main():
    container = Container()
    container.wire([__name__])
    await container.init_resources()
    generator = async_generator()
    resource = await anext(generator)
    assert isinstance(resource, Resource), f"resource is of type {type(resource)}"

asyncio.run(main())

tkuenzle avatar Jul 08 '22 12:07 tkuenzle

the same problem

alexted avatar Sep 14 '23 21:09 alexted

Same issue.

What is the maintenance status of this lib? @rmk135

This issue seems to be important to be open for so long

fabiocerqueira avatar Dec 08 '23 18:12 fabiocerqueira

+1 here. Hitting it as well.

iocentos avatar Jan 11 '24 09:01 iocentos