Python 3.9: "async_generator can't be used in 'await' expression" when using yield in __anext__

I'm trying the following:

class Payload_Session_Generator: def __init__(self): pass async def __anext__(self): async for payload in generate_fb_payload(): if type(payload) != str: yield payload else: StopAsyncIteration def __aiter__(self): return self

This is then passed as an instance to a different function and the __aiter__ method is called explicitly and _iter is an object of the above class:

chunk = await self._iter.__anext__()

This then yields the following error:

TypeError: object async_generator can't be used in 'await' expression

1

2 Answers

Change your code like

class PayloadSessionGenerator: def __init__(self): pass async def __aiter__(self): async for payload in generate_fb_payload(): if type(payload) != str: yield payload else: StopAsyncIteration

try next call

x = Payload_Session_Generator()
chunk = await x.__anext__()
0

An asynchronous generator is similar to a regular generator: __anext__ must provide one element each time it is called. Since an (async) generator is an (async) iterator, it must return the object itself.

As such, __anext__ should use return instead of yield:

class Payload_Session_Generator: async def __anext__(self): async for payload in generate_fb_payload(): if type(payload) != str: return payload # return one element else: raise StopAsyncIteration # raise at end of iteration def __aiter__(self): return self

Note that if a class uses just __anext__ and __aiter__, it is simpler to write a proper async generator. An async generator is defined using async def and yield in the body:

async def payload_session_generator(self): async for payload in generate_fb_payload(): if type(payload) != str: yield payload # yield each element else: break # exiting ends iteration

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like