Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async Resource depends on another async Resource: failure #361

Closed
kolypto opened this issue Jan 16, 2021 · 2 comments
Closed

async Resource depends on another async Resource: failure #361

kolypto opened this issue Jan 16, 2021 · 2 comments
Assignees
Labels

Comments

@kolypto
Copy link

kolypto commented Jan 16, 2021

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())
@rmk135 rmk135 self-assigned this Jan 16, 2021
@rmk135 rmk135 added the bug label Jan 16, 2021
@rmk135
Copy link
Member

rmk135 commented Jan 16, 2021

Hi @kolypto . Yep, that looks like a bug. Thanks for filing the issue. I’ll take a look.

@rmk135
Copy link
Member

rmk135 commented Jan 19, 2021

Fixed in 4.10.2. Many thanks for reporting the bug. Closing the issue, reopen if needed.

@rmk135 rmk135 closed this as completed Jan 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants