Skip to content

Commit 032787d

Browse files
authored
builtins: accept old-style iterables to iter (#7817)
1 parent ac30b96 commit 032787d

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

stdlib/builtins.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,9 +1202,15 @@ def help(request: object = ...) -> None: ...
12021202
def hex(__number: int | SupportsIndex) -> str: ...
12031203
def id(__obj: object) -> int: ...
12041204
def input(__prompt: object = ...) -> str: ...
1205+
1206+
class _GetItemIterable(Protocol[_T_co]):
1207+
def __getitem__(self, __i: int) -> _T_co: ...
1208+
12051209
@overload
12061210
def iter(__iterable: SupportsIter[_SupportsNextT]) -> _SupportsNextT: ...
12071211
@overload
1212+
def iter(__iterable: _GetItemIterable[_T]) -> Iterator[_T]: ...
1213+
@overload
12081214
def iter(__function: Callable[[], _T | None], __sentinel: None) -> Iterator[_T]: ...
12091215
@overload
12101216
def iter(__function: Callable[[], _T], __sentinel: object) -> Iterator[_T]: ...
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from typing import Iterator
2+
from typing_extensions import assert_type
3+
4+
5+
class OldStyleIter:
6+
def __getitem__(self, index: int) -> str:
7+
return str(index)
8+
9+
10+
for x in iter(OldStyleIter()):
11+
assert_type(x, str)
12+
13+
assert_type(iter(OldStyleIter()), Iterator[str])
14+
assert_type(next(iter(OldStyleIter())), str)

0 commit comments

Comments
 (0)