builtins: accept old-style iterables to iter (#7817)

This commit is contained in:
Shantanu
2022-05-10 00:06:31 -07:00
committed by GitHub
parent ac30b96d14
commit 032787d867
2 changed files with 20 additions and 0 deletions

View File

@@ -1202,9 +1202,15 @@ def help(request: object = ...) -> None: ...
def hex(__number: int | SupportsIndex) -> str: ...
def id(__obj: object) -> int: ...
def input(__prompt: object = ...) -> str: ...
class _GetItemIterable(Protocol[_T_co]):
def __getitem__(self, __i: int) -> _T_co: ...
@overload
def iter(__iterable: SupportsIter[_SupportsNextT]) -> _SupportsNextT: ...
@overload
def iter(__iterable: _GetItemIterable[_T]) -> Iterator[_T]: ...
@overload
def iter(__function: Callable[[], _T | None], __sentinel: None) -> Iterator[_T]: ...
@overload
def iter(__function: Callable[[], _T], __sentinel: object) -> Iterator[_T]: ...

View File

@@ -0,0 +1,14 @@
from typing import Iterator
from typing_extensions import assert_type
class OldStyleIter:
def __getitem__(self, index: int) -> str:
return str(index)
for x in iter(OldStyleIter()):
assert_type(x, str)
assert_type(iter(OldStyleIter()), Iterator[str])
assert_type(next(iter(OldStyleIter())), str)