Skip to content

Fix typing of reversed #10655

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

Merged
merged 19 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1634,9 +1634,9 @@ def quit(code: sys._ExitCode = None) -> NoReturn: ...

class reversed(Iterator[_T]):
@overload
def __init__(self, __sequence: Reversible[_T]) -> None: ...
def __new__(cls, __sequence: Reversible[_T]) -> Iterator[_T]: ... # type: ignore[misc]
@overload
def __init__(self, __sequence: SupportsLenAndGetItem[_T]) -> None: ...
def __new__(cls, __sequence: SupportsLenAndGetItem[_T]) -> Iterator[_T]: ... # type: ignore[misc]
def __iter__(self) -> Self: ...
def __next__(self) -> _T: ...
def __length_hint__(self) -> int: ...
Expand Down
34 changes: 34 additions & 0 deletions test_cases/stdlib/builtins/check_reversed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from __future__ import annotations

from collections.abc import Iterator
from typing import Generic, TypeVar
from typing_extensions import assert_type

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


class MyReversible:
def __iter__(self) -> Iterator[str]:
yield "blah"

def __reversed__(self) -> Iterator[str]:
yield "blah"


assert_type(list(reversed(MyReversible())), "list[str]")


_T = TypeVar("_T")


class MyLenAndGetItem(Generic[_T]):
def __len__(self) -> int:
return 0

def __getitem__(self, item: int) -> _T:
raise KeyError


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