Skip to content

async Resource depends on another async Resource: failure #361

@kolypto

Description

@kolypto

Hi, thanks for a wonderful project :)

I just discovered that the following container:

class Container(containers.DeclarativeContainer):
    config = providers.Configuration()

    # Define two resources
    db_connection = providers.Resource(
        db_connection,
        db_url=config.db_url,
    )

    # The second resource depends on the first one
    user_session = providers.Resource(
        user_session,
        db=db_connection
    )

Result:

File "src/dependency_injector/providers.pyx", line 168, in dependency_injector.providers.Provider.__call__
File "src/dependency_injector/providers.pyx", line 2970, in dependency_injector.providers.Resource._provide
AttributeError: '_asyncio.Future' object has no attribute '__anext__'

The error goes away if I remove async from the first resource, of from both of them.

Full source code with reproducible error, using version 4.10.0:

import asyncio
from dependency_injector import containers, providers


async def db_connection(db_url: str):
    yield {'connection': 'ok', 'url': db_url}


async def user_session(db = None):
    yield {'session': 'ok', 'db': db}


class Container(containers.DeclarativeContainer):
    config = providers.Configuration()

    # Define two resources, one depending upon another
    db_connection = providers.Resource(
        db_connection,
        db_url=config.db_url,
    )

    user_session = providers.Resource(
        user_session,
        db=db_connection
    )


if __name__ == '__main__':
    # Init the container
    container = Container(
        config={'db_url': 'postgres://...'}
    )

    # Run
    async def main():
        try:
            # Get the resource
            user = await container.user_session()
            print('user', user)
        finally:
            await container.shutdown_resources()

    asyncio.run(main())

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions