mirror of
https://github.com/davidhalter/typeshed.git
synced 2026-01-09 04:52:23 +08:00
Allow reversed to take any object implementing __len__ and __getitem__ (#5820)
`reversed` is currently annotated as accepting either a `Sequence` or objects implementing the `__reversed__` protocol. This however is too strict as, per its [docs](https://docs.python.org/3/library/functions.html#reversed), it can take any object that implements `__len__` and `__getitem__`.
This commit is contained in:
@@ -37,6 +37,10 @@ class SupportsDivMod(Protocol[_T_contra, _T_co]):
|
||||
class SupportsRDivMod(Protocol[_T_contra, _T_co]):
|
||||
def __rdivmod__(self, __other: _T_contra) -> _T_co: ...
|
||||
|
||||
class SupportsLenAndGetItem(Protocol[_T_co]):
|
||||
def __len__(self) -> int: ...
|
||||
def __getitem__(self, __k: int) -> _T_co: ...
|
||||
|
||||
# Mapping-like protocols
|
||||
|
||||
# stable
|
||||
|
||||
@@ -11,6 +11,7 @@ from _typeshed import (
|
||||
StrOrBytesPath,
|
||||
SupportsDivMod,
|
||||
SupportsKeysAndGetItem,
|
||||
SupportsLenAndGetItem,
|
||||
SupportsLessThan,
|
||||
SupportsLessThanT,
|
||||
SupportsRDivMod,
|
||||
@@ -1286,10 +1287,10 @@ else:
|
||||
def quit(code: object = ...) -> NoReturn: ...
|
||||
|
||||
class reversed(Iterator[_T], Generic[_T]):
|
||||
@overload
|
||||
def __init__(self, __sequence: Sequence[_T]) -> None: ...
|
||||
@overload
|
||||
def __init__(self, __sequence: Reversible[_T]) -> None: ...
|
||||
@overload
|
||||
def __init__(self, __sequence: SupportsLenAndGetItem[_T]) -> None: ...
|
||||
def __iter__(self) -> Iterator[_T]: ...
|
||||
def __next__(self) -> _T: ...
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
from typing import Sized, overload
|
||||
from typing_extensions import Literal, Protocol
|
||||
from _typeshed import SupportsLenAndGetItem
|
||||
from typing import overload
|
||||
from typing_extensions import Literal
|
||||
|
||||
class _Vector(Protocol, Sized):
|
||||
def __getitem__(self, i: int) -> float: ...
|
||||
_Vector = SupportsLenAndGetItem[float]
|
||||
|
||||
class AnnoyIndex:
|
||||
f: int
|
||||
|
||||
Reference in New Issue
Block a user