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 resources providers in list providers #450

Closed
mxab opened this issue Apr 21, 2021 · 3 comments
Closed

Async resources providers in list providers #450

mxab opened this issue Apr 21, 2021 · 3 comments
Assignees
Labels

Comments

@mxab
Copy link

mxab commented Apr 21, 2021

Hi,
I'm trying to use async resources with list providers but it doesn't seem to work

from typing import List
from dependency_injector import containers,providers
import asyncio
class MyResource:
    def __init__(self,my_param:str) -> None:
        self.my_param = my_param

async def create_resource(param:str):
    return MyResource(my_param=param)


class Printer:
    def __init__(self, resources: List[MyResource]) -> None:
        self.resources = resources
    def print(self):
        for r in self.resources:
            print(r.my_param)

class Container(containers.DeclarativeContainer):

    my_resources = providers.List(
        providers.Resource(create_resource, "foo"),
        providers.Resource(create_resource, "bar")
        )
    
    printer = providers.Factory(Printer, resources = my_resources)


async def main():
    container = Container()
    printer = await container.printer()

    printer.print()


if __name__ == "__main__":
    asyncio.get_event_loop().run_until_complete(main())

if I remove the async from the create_resource function and the await infront of container.printer() it works.

Is there anything I missed or is it currently not supported?

@rmk135 rmk135 self-assigned this Apr 25, 2021
@rmk135
Copy link
Member

rmk135 commented Apr 25, 2021

Hey @mxab ,

Yeah, this is a bug. Thanks for reporting this. Working on the fix.

@rmk135 rmk135 added the bug label Apr 25, 2021
@rmk135
Copy link
Member

rmk135 commented Apr 25, 2021

@mxab Fixed in 4.32.1. Thanks again for bringing this up.

@rmk135 rmk135 closed this as completed Apr 25, 2021
@mxab
Copy link
Author

mxab commented Apr 26, 2021

Nice! Will try today

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