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
fromtyping_extensionsimportBufferclassClassA:
defaccept_buffer2(self, buffer: Buffer):
passa=ClassA()
fromarrayimportarrayimportnumpyasnparray_buffer=array("l", [1, 2, 3, 4, 5])
np_buffer=np.array([1, 2, 3, 4, 5], dtype=np.int32)
print(memoryview(array_buffer))
# typing issueprint(memoryview(np_buffer))
a.accept_buffer2(array_buffer)
# Argument of type "NDArray[signedinteger[_32Bit]]" cannot be assigned to parameter "buffer" of type "Buffer" in function "accept_buffer2"# "ndarray[Any, dtype[signedinteger[_32Bit]]]" is incompatible with protocol "Buffer"# "__buffer__" is not presenta.accept_buffer2(np_buffer)
In Python 3.11 it will fail as __buffer__ in a stub file is only implemented for Python 3.12:
But technically it buffer protocol is implemented before Python 3.11 but it's just done internally, not using __buffer__.
__buffer__ dunder seems to be the way Buffer checks whether some class has implemented buffer protocol and Buffer is available not only in Python 3.12 with collections.abc.Buffer but for older python versions too using typing_extensions.Buffer.
In 3.12 np.ndarray.__buffer__ is indeed accessable and in <3.11 it isn't but standard library's array.array has a similar issue and they implement __buffer__ for convenience.
Uh oh!
There was an error while loading. Please reload this page.
Proposed new feature or change:
Consider the snippet below.
In Python 3.11 it will fail as
__buffer__
in a stub file is only implemented for Python 3.12:numpy/numpy/__init__.pyi
Lines 1462 to 1463 in 7687245
But technically it buffer protocol is implemented before Python 3.11 but it's just done internally, not using
__buffer__
.__buffer__
dunder seems to be the wayBuffer
checks whether some class has implemented buffer protocol andBuffer
is available not only in Python 3.12 withcollections.abc.Buffer
but for older python versions too usingtyping_extensions.Buffer
.In 3.12
np.ndarray.__buffer__
is indeed accessable and in <3.11 it isn't but standard library'sarray.array
has a similar issue and they implement__buffer__
for convenience.See
array.array
stub file and related PR python/typeshed#10225https://github.com/python/typeshed/blob/434f6528b7607d9ae6c3d34329840a18922df925/stdlib/array.pyi#L87
I've submitted a small PR #26784 that resolves this.
The text was updated successfully, but these errors were encountered: