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

using Container.get on async factories leads to confusing behaviour #21

Closed
elfjes opened this issue Aug 8, 2023 · 1 comment
Closed

Comments

@elfjes
Copy link

elfjes commented Aug 8, 2023

async factories are supposed to be invoked using await Container.aget. However, currently there's nothing stopping a user from using the normal Container.get method. For example, this works for an async factory:

async def int_factory():
    await asyncio.sleep(0)
    return 42

registry.register_factory(int, int_factory)
await container.get(int) # obtains 42 even though the sync .get method was used

A user that doesn't know about aget might think all is well. But all is not well, since the value stored in the container is not 42 but the coroutine, which has now been awaited.

Solution could be to detect if .get is used for an async factory and raise an error. Luckily there already is the RegisteredService.is_async flag to help out with that

@hynek hynek closed this as completed in 5f6a016 Aug 8, 2023
@hynek
Copy link
Owner

hynek commented Aug 8, 2023

fix is in 23.11!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants