8000 Merge pull request #24707 from charris/backport-24705 · numpy/numpy@ca97802 · GitHub
[go: up one dir, main page]

Skip to content

Commit ca97802

Browse files
authored
Merge pull request #24707 from charris/backport-24705
TYP: Add annotations for the py3.12 buffer protocol
2 parents 2e84b15 + 92aab8c commit ca97802

File tree

4 files changed

+50
-23
lines changed

4 files changed

+50
-23
lines changed

numpy/__init__.pyi

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import builtins
2+
import sys
23
import os
34
import mmap
45
import ctypes as ct
@@ -1440,17 +1441,18 @@ _ShapeType = TypeVar("_ShapeType", bound=Any)
14401441
_ShapeType2 = TypeVar("_ShapeType2", bound=Any)
14411442
_NumberType = TypeVar("_NumberType", bound=number[Any])
14421443

1443-
# There is currently no exhaustive way to type the buffer protocol,
1444-
# as it is implemented exclusively in the C API (python/typing#593)
1445-
_SupportsBuffer = Union[
1446-
bytes,
1447-
bytearray,
1448-
memoryview,
1449-
_array.array[Any],
1450-
mmap.mmap,
1451-
NDArray[Any],
1452-
generic,
1453-
]
1444+
if sys.version_info >= (3, 12):
1445+
from collections.abc import Buffer as _SupportsBuffer
1446+
else:
1447+
_SupportsBuffer = (
1448+
bytes
1449+
| bytearray
1450+
| memoryview
1451+
| _array.array[Any]
1452+
| mmap.mmap
1453+
| NDArray[Any]
1454+
| generic
1455+
)
14541456

14551457
_T = TypeVar("_T")
14561458
_T_co = TypeVar("_T_co", covariant=True)
@@ -1513,6 +1515,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]):
15131515
order: _OrderKACF = ...,
15141516
) -> _ArraySelf: ...
15151517

1518+
if sys.version_info >= (3, 12):
1519+
def __buffer__(self, flags: int, /) -> memoryview: ...
1520+
15161521
def __class_getitem__(self, item: Any) -> GenericAlias: ...
15171522

15181523
@overload
@@ -2570,6 +2575,9 @@ class generic(_ArrayOrScalarCommon):
25702575
@property
25712576
def flat(self: _ScalarType) -> flatiter[ndarray[Any, _dtype[_ScalarType]]]: ...
25722577

2578+
if sys.version_info >= (3, 12):
2579+
def __buffer__(self, flags: int, /) -> memoryview: ...
2580+
25732581
@overload
25742582
def astype(
25752583
self,
@@ -2772,6 +2780,9 @@ class object_(generic):
27722780
def __float__(self) -> float: ...
27732781
def __complex__(self) -> complex: ...
27742782

2783+
if sys.version_info >= (3, 12):
2784+
def __release_buffer__(self, buffer: memoryview, /) -> None: ...
2785+
27752786
# The `datetime64` constructors requires an object with the three attributes below,
27762787
# and thus supports datetime duck typing
27772788
class _DatetimeScalar(Protocol):

numpy/_typing/_array_like.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from __future__ import annotations
22

3+
import sys
34
from collections.abc import Collection, Callable, Sequence
45
from typing import Any, Protocol, Union, TypeVar, runtime_checkable
6+
57
from numpy import (
68
ndarray,
79
dtype,
@@ -76,17 +78,18 @@ def __array_function__(
7678
_NestedSequence[_T],
7779
]
7880

79-
# TODO: support buffer protocols once
80-
#
81-
# https://bugs.python.org/issue27501
82-
#
83-
# is resolved. See also the mypy issue:
84-
#
85-
# https://github.com/python/typing/issues/593
86-
ArrayLike = _DualArrayLike[
87-
dtype[Any],
88-
Union[bool, int, float, complex, str, bytes],
89-
]
81+
if sys.version_info >= (3, 12):
82+
from collections.abc import Buffer
83+
84+
ArrayLike = Buffer | _DualArrayLike[
85+
dtype[Any],
86+
Union[bool, int, float, complex, str, bytes],
87+
]
88+
else:
89+
ArrayLike = _DualArrayLike[
90+
dtype[Any],
91+
Union[bool, int, float, complex, str, bytes],
92+
]
9093

9194
# `ArrayLike<X>_co`: array-like objects that can be coerced into `X`
9295
# given the casting rules `same_kind`

numpy/array_api/_typing.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"PyCapsule",
1818
]
1919

20+
import sys
21+
2022
from typing import (
2123
Any,
2224
Literal,
@@ -63,8 +65,11 @@ def __len__(self, /) -> int: ...
6365
float64,
6466
]]
6567

68+
if sys.version_info >= (3, 12):
69+
from collections.abc import Buffer as SupportsBufferProtocol
70+
else:
71+
SupportsBufferProtocol = Any
6672

67-
SupportsBufferProtocol = Any
6873
PyCapsule = Any
6974

7075
class SupportsDLPack(Protocol):

numpy/typing/tests/data/reveal/array_constructors.pyi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,11 @@ assert_type(np.stack([A, A], out=B), SubClass[np.float64])
211211

212212
assert_type(np.block([[A, A], [A, A]]), npt.NDArray[Any])
213213
assert_type(np.block(C), npt.NDArray[Any])
214+
215+
if sys.version_info >= (3, 12):
216+
from collections.abc import Buffer
217+
218+
def create_array(obj: npt.ArrayLike) -> npt.NDArray[Any]: ...
219+
220+
buffer: Buffer
221+
assert_type(create_array(buffer), npt.NDArray[Any])

0 commit comments

Comments
 (0)
0