193 – Typing generators#
Generator type hints are cumbersome to write because the generic type Generator takes three arguments:
from collections.abc import Generator
def simple_generator(stop: int) -> Generator[int, None, None]:
for x in range(stop):
yield x ** 2
The three types indicate the type of the yielded values, the type of the send values, and the type of the return.
In Python 3.13, the send and return types were changed so that they default to None, allowing you to simplify many usages of Generator:
from collections.abc import Generator
def simple_generator(stop: int) -> Generator[int]:
for x in range(stop):
yield x ** 2
The omission of the second and third types indicate that they are None.