ryananguiano / async_property

Python decorator for async properties.
MIT License
82 stars 8 forks source link

Support async generators ? #7

Open asmodehn opened 4 years ago

asmodehn commented 4 years ago

Description

I'm trying to use async_property on an async generator. The goal is to allow using a property on an async loop constructor like so:

myclass_instance = MyClass()
async for msg in myclass_instance.myqueue:
    print(msg)

What I Did

class MyClass:

    self._queue: asyncio.Queue

    # --snip--

    @async_property
    async def myqueue(self):
        while True:
            yield await self._queue.get()  # retrieves message async queue (potentially awaits)

Error:

  File "--snip--/lib/python3.8/site-packages/async_property/base.py", line 10, in async_property
    assert is_coroutine(func), 'Can only use with async def'
AssertionError: Can only use with async def

Any hint on the best way to do that ? I can make a PR if needed.