mirror of
https://github.com/davidhalter/typeshed.git
synced 2025-12-08 04:54:47 +08:00
builtins: Fix typing of reversed (#10655)
This commit is contained in:
@@ -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: ...
|
||||
|
||||
34
test_cases/stdlib/builtins/check_reversed.py
Normal file
34
test_cases/stdlib/builtins/check_reversed.py
Normal file
@@ -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]")
|
||||
Reference in New Issue
Block a user