Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix annotations for builtins.reversed.__new__ #11646

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ from _typeshed import (
SupportsRichComparisonT,
SupportsWrite,
)
from collections.abc import Awaitable, Callable, Iterable, Iterator, MutableSet, Reversible, Set as AbstractSet, Sized
from collections.abc import Awaitable, Callable, Iterable, Iterator, MutableSet, Set as AbstractSet, Sized
from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper
from types import CodeType, TracebackType, _Cell

Expand Down Expand Up @@ -1636,13 +1636,16 @@ def pow(base: _SupportsSomeKindOfPow, exp: float, mod: None = None) -> Any: ...
def pow(base: _SupportsSomeKindOfPow, exp: complex, mod: None = None) -> complex: ...
def quit(code: sys._ExitCode = None) -> NoReturn: ...

class reversed(Iterator[_T]):
class _SupportsReversed(Protocol[_T_co]):
def __reversed__(self) -> _T_co: ...

class reversed(Iterator[_T_co]):
@overload
def __new__(cls, sequence: Reversible[_T], /) -> Iterator[_T]: ... # type: ignore[misc]
def __new__(cls, sequence: _SupportsReversed[_T], /) -> _T: ... # type: ignore[misc]
@overload
def __new__(cls, sequence: SupportsLenAndGetItem[_T], /) -> Iterator[_T]: ... # type: ignore[misc]
def __new__(cls, sequence: SupportsLenAndGetItem[_T_co], /) -> Self: ...
def __iter__(self) -> Self: ...
def __next__(self) -> _T: ...
def __next__(self) -> _T_co: ...
def __length_hint__(self) -> int: ...

def repr(obj: object, /) -> str: ...
Expand Down
16 changes: 15 additions & 1 deletion test_cases/stdlib/builtins/check_reversed.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,18 @@ def __getitem__(self, item: int) -> _T:


len_and_get_item: MyLenAndGetItem[int] = MyLenAndGetItem()
assert_type(list(reversed(len_and_get_item)), "list[int]")
assert_type(reversed(len_and_get_item), "reversed[int]")


class UnTrue:
def __reversed__(self) -> UnFalse:
return UnFalse()


class UnFalse:
def __reversed__(self) -> UnTrue:
return UnTrue()


assert_type(reversed(UnTrue()), "UnFalse")
assert_type(reversed(reversed(UnTrue())), "UnTrue")