Description
Bug report
Bug description:
The memoryview
builtin is registered as a Sequence
, but doesn't implement the full API, because it doesn't inherit from Sequence
at runtime and doesn't implement the mixin methods.
>>> import collections.abc
>>> issubclass(memoryview, collections.abc.Sequence)
True
>>> collections.abc.Sequence.index
<function Sequence.index at 0x10148d250>
>>> memoryview.index
Traceback (most recent call last):
File "<python-input-4>", line 1, in <module>
memoryview.index
AttributeError: type object 'memoryview' has no attribute 'index'
Among the methods listed for Sequence in the documentation, memoryview is missing __contains__
, __reversed__
, index
, and count
. This is causing problems for typing; in the typeshed stubs we have to either lie one way by claiming that memoryview has methods it doesn't have, or lie another way by claiming it is not a Sequence when issubclass()
says otherwise at runtime (python/typeshed#12800). To fix this, we should either make memoryview not a Sequence, or add the missing methods. The former has compatibility implications, so I propose to add the methods in 3.14.
CPython versions tested on:
CPython main branch
Operating systems tested on:
No response
Linked PRs
- gh-125420: implement
Sequence.count
API onmemoryview
objects #125443 - gh-125420: implement
Sequence.index
API onmemoryview
objects #125446 - (under consideration) gh-125420: implement
Sequence.__contains__
API onmemoryview
objects #125441 - (under consideration) gh-125420: implement
Sequence.__reversed__
API onmemoryview
objects #125505