8000 Injecting an async resource into an async generator provides future instead of resource · Issue #603 · ets-labs/python-dependency-injector · GitHub
[go: up one dir, main page]

Skip to content
Injecting an async resource into an async generator provides future instead of resource #603
@tkuenzle

Description

@tkuenzle

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())

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0