Allow Return Statements with Values in Asynchronous Generators

Okay I’ll work on this and should have a full reference implementation soon :slight_smile:

2 Likes

I look forward to seeing a reference implementation, I see some potential value here.

However, there’s sort of two conditions in my mind.

  1. synchronous generators are guaranteed to be cleaned up in cpython, the same guarantee would need to exist for asynchronous generators (see background in pep 533 and mentions above)
  2. yield from has to work so that the return value is actually meaningful.

The first of those was already deferred before, but I don’t think async yield from is going to improve the ecosystem if it requires wrapping everything in context managers anyway.

Returning value from generator was implemented to make yield from returning value. yield from was added to implement early forms of asynchronous code. But now this is replaced by await and async, etc, so most of use cases of yield from are gone. Internally, await is implemented via the same mechanism as yield from. This is why yield from does not work with asynchronous generators. Thus, there is no need to return value from asynchronous generator.

Since yield from does not work with asynchronous generators, you need to use except StopAsyncIteration if this feature was implemented. But you already achieve the same result by raising an exception.

1 Like

There is a PR that implements this here: