["python","python-typing"]
How can I annotate a generator in Python 3?

Python 3.x supports (optional) function annotations: def add_ints(x: int, y: int) -> int: return x + y I sometimes encounter problems as to how to represent a given "type", and this time, I have a function that returns a generator: def myfunc(x: [int]) -> "generator that returns ints": # ^~~~~~~~~~~~~~~~~~~~~~~~~~ return (n for n in x if n % 2 == 0) How should I annotate the return value?
The collections.abc module defines the collections.abc.Generator type, which you can use like: Generator[yield_type, send_type, return_type] where send_type and return_type default to None. (If your generator neither sends nor returns, it can just be annotated as Generator[yield_type]. Though it has been deprecated since 3.9, typing.Generator behaves very similarly.