Skip to content

Commit 1179a27

Browse files
ngaya-llilevkivskyi
authored andcommitted
Use Iterator in documentation examples for generator functions (#6896)
The current "cheat sheet" examples use `Iterable` to annotate the return type of generator functions. This PR changes this to `Iterator`, which is more specific.
1 parent c352d0c commit 1179a27

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

docs/source/cheat_sheet.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Functions
5858

5959
.. code-block:: python
6060
61-
from typing import Callable, Iterable, Union, Optional, List
61+
from typing import Callable, Iterator, Union, Optional, List
6262
6363
# This is how you annotate a function definition
6464
def stringify(num):
@@ -95,9 +95,9 @@ Functions
9595
x = f # type: Callable[[int, float], float]
9696
9797
# A generator function that yields ints is secretly just a function that
98-
# returns an iterable (see below) of ints, so that's how we annotate it
99-
def f(n):
100-
# type: (int) -> Iterable[int]
98+
# returns an iterator of ints, so that's how we annotate it
99+
def g(n):
100+
# type: (int) -> Iterator[int]
101101
i = 0
102102
while i < n:
103103
yield i

docs/source/cheat_sheet_py3.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Python 3 supports an annotation syntax for function declarations.
8686

8787
.. code-block:: python
8888
89-
from typing import Callable, Iterable, Union, Optional, List
89+
from typing import Callable, Iterator, Union, Optional, List
9090
9191
# This is how you annotate a function definition
9292
def stringify(num: int) -> str:
@@ -104,8 +104,8 @@ Python 3 supports an annotation syntax for function declarations.
104104
x: Callable[[int, float], float] = f
105105
106106
# A generator function that yields ints is secretly just a function that
107-
# returns an iterable (see below) of ints, so that's how we annotate it
108-
def f(n: int) -> Iterable[int]:
107+
# returns an iterator of ints, so that's how we annotate it
108+
def g(n: int) -> Iterator[int]:
109109
i = 0
110110
while i < n:
111111
yield i

0 commit comments

Comments
 (0)