Asynchronous generators (PEP 525) and comprehensions (PEP 530)

Python 3.6 allows coroutines defined with async def (PEP 492) to begenerators, i.e. contain yield expressions. It also introduced a syntax forasynchronous comprehensions. This example uses the AsyncIterator type todefine an async generator:

  1. from typing import AsyncIterator
  2.  
  3. async def gen() -> AsyncIterator[bytes]:
  4. lst = [b async for b in gen()] # Inferred type is "List[bytes]"
  5. yield 'no way' # Error: Incompatible types (got "str", expected "bytes")