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:
- from typing import AsyncIterator
- async def gen() -> AsyncIterator[bytes]:
- lst = [b async for b in gen()] # Inferred type is "List[bytes]"
- yield 'no way' # Error: Incompatible types (got "str", expected "bytes")