From 193921024e3aaf9568169564e366575fd0bdf785 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Thu, 30 Sep 2021 19:09:57 +0200 Subject: [PATCH] Add SimpleSequence as preferred alias for SupportsLenAndGetItem This protocol represents a sequence as defined in the Python documentation, as opposed to collections.abc.Sequence. --- stdlib/_typeshed/__init__.pyi | 9 +++++++++ stdlib/builtins.pyi | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/stdlib/_typeshed/__init__.pyi b/stdlib/_typeshed/__init__.pyi index 9e60fd07e017..0bf46b47934b 100644 --- a/stdlib/_typeshed/__init__.pyi +++ b/stdlib/_typeshed/__init__.pyi @@ -46,6 +46,15 @@ class SupportsDivMod(Protocol[_T_contra, _T_co]): class SupportsRDivMod(Protocol[_T_contra, _T_co]): def __rdivmod__(self, __other: _T_contra) -> _T_co: ... +# This represents a sequence as defined in the Python documentation: +# https://docs.python.org/3/glossary.html#term-sequence +# collections.abc.Sequence and typing.Sequence are not protocols and +# have additional fields that are often unnecessary. +class SimpleSequence(Protocol[_T_co]): + def __len__(self) -> int: ... + def __getitem__(self, __k: int) -> _T_co: ... + +# obsolete, use SimpleSequence instead class SupportsLenAndGetItem(Protocol[_T_co]): def __len__(self) -> int: ... def __getitem__(self, __k: int) -> _T_co: ... diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index fe8b35ac5100..92da8725a4ae 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -8,11 +8,11 @@ from _typeshed import ( OpenTextMode, ReadableBuffer, Self, + SimpleSequence, StrOrBytesPath, SupportsAnext, SupportsDivMod, SupportsKeysAndGetItem, - SupportsLenAndGetItem, SupportsLessThan, SupportsLessThanT, SupportsNext, @@ -1293,7 +1293,7 @@ class reversed(Iterator[_T], Generic[_T]): @overload def __init__(self, __sequence: Reversible[_T]) -> None: ... @overload - def __init__(self, __sequence: SupportsLenAndGetItem[_T]) -> None: ... + def __init__(self, __sequence: SimpleSequence[_T]) -> None: ... def __iter__(self) -> Iterator[_T]: ... def __next__(self) -> _T: ...