ets-labs / python-dependency-injector

Dependency injection framework for Python
https://python-dependency-injector.ets-labs.org/
BSD 3-Clause "New" or "Revised" License
4.01k stars 306 forks source link

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

Open tkuenzle opened 2 years ago

tkuenzle commented 2 years ago

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())
alexted commented 1 year ago

the same problem

fabiocerqueira commented 12 months ago

Same issue.

What is the maintenance status of this lib? @rmk135

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

iocentos commented 10 months ago

+1 here. Hitting it as well.

JaviFuentes94 commented 3 months ago

+1

smandava commented 2 months ago

same issue. is there a work around for this?