Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Type Alias

Generator

Generator is a generic type alias for annotating generator functions. Its parameters describe yielded values, values accepted by send(), and the final return value.

How it works

Generator is a generic type alias for describing generator functions. Its three parameters specify the type produced by yield, the type accepted by send, and the type returned when the generator terminates.

Syntax

Generator[YieldType, SendType, ReturnType]

Parameters

NameTypeDescription
YieldTypetypeType produced by yield expressions.
SendTypetypeType accepted by generator.send().
ReturnTypetypeType returned when the generator finishes.

Returns

A parameterized typing description of a generator: Generator[YieldType, SendType, ReturnType].

Key points

  • Write Generator with three type arguments: yielded value type, sent value type, and final return type.
  • Generator annotations communicate intent to static-analysis tools rather than enforcing runtime checks.
  • Type checkers, IDEs, and linters can use Generator annotations to detect inconsistent generator usage.
  • Annotate the generator function's return type with Generator rather than annotating each yield expression separately.

Common mistakes

  • The annotation guides static analysis but does not make Python check yielded, sent, or returned values at runtime.
  • SendType follows contravariant rules, which matters when a generator type is used in a subtype relationship.

Example: Annotating a generator

from typing import Generator


def countdown(start: int) -> Generator[int, None, str]:
    for value in range(start, 0, -1):
        yield value
    return "done"


iterator = countdown(3)
print(list(iterator))

The annotation declares yielded integers, values sent into the generator as None, and a string returned when iteration finishes.

When to use it

Typed lazy iteration

A function yields a sequence lazily while exposing its produced element type.

Bidirectional generator protocols

A coroutine-like generator receives values through send() and yields intermediate results.

Typed generator completion

A generator returns a meaningful result after its final yield.

Interview questions

QWhat is the parameter order for Generator?

Use Generator[YieldType, SendType, ReturnType]. For example, Generator[int, float, str] describes a generator that yields integers, accepts floats through send(), and returns a string when exhausted.

Follow-up: What does the second parameter describe? It is the type accepted by send(), not the type yielded by the generator.

QWhat do Generator's three type parameters represent?

The first parameter describes values produced by yield, the second describes values accepted by send(), and the third describes the generator's eventual return value. A return value is delivered through StopIteration when iteration ends.

Follow-up: How should an ordinary generator that never receives values be annotated? Use None as SendType, such as Generator[int, None, str].

QDoes Python enforce a Generator annotation at runtime?

Generator is intended for annotating generator functions and is parameterized as Generator[YieldType, SendType, ReturnType]. Type annotations are available to tools such as type checkers, IDEs, and linters, but Python does not enforce them during execution.

Follow-up: Does the annotation change generator execution? No; it supplies typing information only and does not alter runtime generator behavior.

  • Iterator
  • AsyncGenerator
  • Generic
  • TypeVar

Source: typing — Support for type hints. Checked 2026-10-16.