ジェネレータ関数の型ヒント
collections.abc.Generator
IMO:
typing.Generator
の代わりに型ヒントに使いたい考え
Pythonのジェネレータ関数の返り値の型ヒントを考える
collections.abc.Iterator
や
collections.abc.Iterable
でも型ヒントできる
返り値(ReturnType)の型だけでアノテーション(ref:
typing.Generator
)
#mypy
のドキュメント(トップの例)にもある
code:example.py
def fib(n: int) -> Iterator
int
:
a, b = 0, 1
while a < n:
yield a
a, b = b, a+b