Typing and Generics

typing.Generator

A synchronous generator with separate yield, send and final return types. Python 3.13 permits omitting the last two type arguments (default None). Deprecated typing alias; prefer collections.abc.Generator in modern code (3.9+).

Formal Syntax & Structure

python syntaxpython
typing.Generator[YieldType, SendType, ReturnType]

Return Value

type expression

Typing construct or runtime result as described; static typing rules are not runtime validation.

Working Example

Example Snippetpython
from typing import Generator
def count()->Generator[int,None,str]:    yield 3    return "done"g=count()first=next(g)try: next(g)except StopIteration as exc: result=exc.valueprint((first,result))
Python Reference