pythongeneratorpython-typing

Type-hinting a generator: send_type Any or None?


I have a generator that does not use send() values. Should I type its send_value as Any or None?

import typing as t

def pi_generator() -> t.Generator[int, ???, None]:
    pi = "3141592"
    for digit in pi:
        yield int(digit)

pi_gen = pi_generator()
next(pi_gen)  # 3
pi_gen.send('foo')  # 1
pi_gen.send(pi_gen)  # 4

Reasons I see for Any:

Reasons I see for None:


Solution

  • Quoting the docs for typing.Generator:

    If your generator will only yield values, set the SendType and ReturnType to None:

    def infinite_stream(start: int) -> Generator[int, None, None]:
       while True:
           yield start
           start += 1
    

    Setting SendType to None is the recommended way to communicate that the generator does not expect callees to send() values.