8000 TYP: Use `collections.abc.Buffer` in the `npt.ArrayLike` definition · numpy/numpy@92aab8c · GitHub
[go: up one dir, main page]

Skip to content

Commit 92aab8c

Browse files
BvB93charris
authored andcommitted
TYP: Use collections.abc.Buffer in the npt.ArrayLike definition
1 parent 56e580d commit 92aab8c

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

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/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