Skip to content

Should Sequence.__getitem__ slice overload return Self? #16791

Open

Description

@loic-simon

The collections.abc.Sequence stub defines __getitem__ as returning a Sequence when passing a slice:

typeshed/stdlib/typing.pyi

Lines 683 to 689 in a817b8d

class Sequence(Reversible[_T_co], Collection[_T_co]):
@overload
@abstractmethod
def __getitem__(self, index: int, /) -> _T_co: ...
@overload
@abstractmethod
def __getitem__(self, index: slice[int | None], /) -> Sequence[_T_co]: ...

According to the data model, slicing a sequence should return the same type (IIUC):

Sequences also support slicing: [...]. When used as an expression, a slice is a sequence of the same type.

So it seem to me that a more precise typing would be

    @overload
    @abstractmethod
    def __getitem__(self, index: slice[int | None], /) -> Self: ...

Which would allow the following (dummy) code to pass type checking:

def get_first_half[S: Sequence](seq: S) -> S:
    return seq[: len(seq) // 2]

Are there issues / drawbacks I did not see? This is a more precise type so it should be backward-compatible, should I open a PR to see what mypy-primer has to say?

Thanks!

Activity

  1. srittau commented on Nov 13, 2026

    @srittau
    Collaborator

    This has used the current return type since antiquity (2015), long before there was Self. An exploratory PR makes sense to me.

  2. added 3 commits that reference this issue on Nov 13, 2026
  3. AlexWaygood commented on Nov 13, 2026

    @AlexWaygood
    Member

    I don't think this should return Self for the same reason that Iterator.__iter__ shouldn't return Self. The most common, and usually recommended, implementation might be to return Self. But these ABCs serve as definitions for fundamental concepts in Python and Python typing. By returning Self, we're redefining Sequence in the type system and saying that something can only be a valid Sequence — can only validly inherit from Sequence without an LSP error from the type checker! — if its __getitem__ method returns Self. That seems like too strict a contract for us to enforce IMO; I don't think that's a reasonable definition of what it means for an object to be a sequence in Python

  4. JelleZijlstra commented on Nov 13, 2026

    @JelleZijlstra
    Member

    Returning Self would be incorrect for heterogeneous tuple types.

  5. srittau commented on Nov 13, 2026

    @srittau
    Collaborator

    I think Jelle's point about heterogeneous tuple types it the main problem. I disagree with Alex, since the docs explicitly say:

    Sequences also support slicing: a[start:stop] selects all items with index k such that start <= k < stop. When used as an expression, a slice is a sequence of the same type.

    But we have no way to say: "Returns the same type, but may use a different specialization." and that makes this (unfortunately) impossible to change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions