You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Should Sequence.__getitem__ slice overload return Self? #16791
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?
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
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.
Activity
srittau commentedon Nov 13, 2026
This has used the current return type since antiquity (2015), long before there was
Self. An exploratory PR makes sense to me.pythonGH-16791: Type Sequence.__getitem__ with slices return Self
pythonGH-16791: Make Sequence.__getitem__ with slices return Self
pythonGH-16791: Make Sequence.__getitem__(slice) return Self
AlexWaygood commentedon Nov 13, 2026
I don't think this should return
Selffor the same reason thatIterator.__iter__shouldn't returnSelf. The most common, and usually recommended, implementation might be to returnSelf. But these ABCs serve as definitions for fundamental concepts in Python and Python typing. By returningSelf, we're redefiningSequencein the type system and saying that something can only be a validSequence— can only validly inherit fromSequencewithout an LSP error from the type checker! — if its__getitem__method returnsSelf. 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 PythonJelleZijlstra commentedon Nov 13, 2026
Returning Self would be incorrect for heterogeneous tuple types.
srittau commentedon Nov 13, 2026
I think Jelle's point about heterogeneous tuple types it the main problem. I disagree with Alex, since the docs explicitly say:
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.