Skip to content

Add SimpleSequence as preferred alias for SupportsLenAndGetItem #6099

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

Closed
wants to merge 2 commits into from
Closed
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
9 changes: 9 additions & 0 deletions stdlib/_typeshed/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just delete it? It was not marked with # stable.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess if it's used in third party stubs (there seems to be the one single case), under some circumstances you could run into silent loss of type checking. This already ends up happening though, e.g. see #5786 and #5751, so I'd also be inclined to just delete it.

If we don't delete it, we should alias here, though, and still update the annoy stubs

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The annoy stubs were my main concern. If we update them, they won't work, until type checkers have been updated with the latest typeshed. I'd suggest keeping it for a few months.

class SupportsLenAndGetItem(Protocol[_T_co]):
def __len__(self) -> int: ...
def __getitem__(self, __k: int) -> _T_co: ...
Expand Down
4 changes: 2 additions & 2 deletions stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ from _typeshed import (
OpenTextMode,
ReadableBuffer,
Self,
SimpleSequence,
StrOrBytesPath,
SupportsAnext,
SupportsDivMod,
SupportsKeysAndGetItem,
SupportsLenAndGetItem,
SupportsNext,
SupportsRDivMod,
SupportsRichComparison,
Expand Down Expand Up @@ -1367,7 +1367,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: ...

Expand Down