PEP 3107 annotation for a generator function

75 views Asked by At

simple question : What is the correct PEP 3107 keyword for a function that yields items ?

Already tried this, but it doesn't work

def this_function() -> generator:
    for x in range(0,100):
        yield x

Also tried with Iterator[int] as this post suggests, but no results. Also tried the various propositions made here, but nothing

N.B. Don't know if this is relevant, but my linter is Pylance on VSCode

1

There are 1 answers

3
Jab On

You can use collections.abc.Generator

from collections.abc import Generator

def this_function() -> Generator[int, None, None]:
    for x in range(0, 100):
        yield x

Or simply: collections.abc.Iterator or collections.abc.Iterable.

from collections.abc import Iterable

def this_function() -> Iterable[int]:
    for x in range(0, 100):
        yield x