Skip to content

Commit 6c7255a

Browse files
committed
🤡 pretend that ndarray and generic implement __buffer__ before py312
1 parent 6de1fac commit 6c7255a

File tree

4 files changed

+18
-39
lines changed

4 files changed

+18
-39
lines changed

‎src/numpy-stubs/__init__.pyi

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ from typing import (
2323
TypedDict,
2424
final,
2525
overload,
26-
runtime_checkable,
2726
type_check_only,
2827
)
29-
from typing_extensions import CapsuleType, LiteralString, Never, Protocol, Self, TypeVar, Unpack, deprecated, override
28+
from typing_extensions import Buffer, CapsuleType, LiteralString, Never, Protocol, Self, TypeVar, Unpack, deprecated, override
3029

3130
from . import (
3231
__config__ as __config__,
@@ -463,22 +462,6 @@ from .lib._utils_impl import get_include, info, show_runtime
463462
from .matrixlib import asmatrix, bmat, matrix
464463
from .version import __version__
465464

466-
@runtime_checkable
467-
class _Buffer(Protocol):
468-
def __buffer__(self, flags: int, /) -> memoryview: ...
469-
470-
if sys.version_info >= (3, 12):
471-
_SupportsBuffer: TypeAlias = _Buffer
472-
else:
473-
import array as _array
474-
import mmap as _mmap
475-
476-
from numpy import distutils as distutils # noqa: ICN003
477-
478-
_SupportsBuffer: TypeAlias = (
479-
_Buffer | bytes | bytearray | memoryview | _array.array[Any] | _mmap.mmap | NDArray[Any] | generic
480-
)
481-
482465
__all__ = [ # noqa: RUF022
483466
# __numpy_submodules__
484467
"char", "core", "ctypeslib", "dtypes", "exceptions", "f2py", "fft", "lib", "linalg", "ma", "polynomial", "random", "rec",
@@ -1993,7 +1976,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
19931976
cls,
19941977
shape: _ShapeLike,
19951978
dtype: DTypeLike = float, # noqa: PYI011
1996-
buffer: _SupportsBuffer | None = None,
1979+
buffer: Buffer | None = None,
19971980
offset: CanIndex = 0,
19981981
strides: _ShapeLike | None = None,
19991982
order: _OrderKACF | None = None,
@@ -3836,9 +3819,6 @@ class generic(_ArrayOrScalarCommon, Generic[_ItemT_co]):
38363819
@abc.abstractmethod
38373820
def __init__(self, /, *args: Any, **kwargs: Any) -> None: ...
38383821

3839-
if sys.version_info >= (3, 12):
3840-
def __buffer__(self, flags: int, /) -> memoryview: ...
3841-
38423822
#
38433823
@overload
38443824
def __array__(self, dtype: None = None, /) -> ndarray[tuple[()], dtype[Self]]: ...

‎src/numpy-stubs/_core/_multiarray_umath.pyi

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ from typing import (
1919
overload,
2020
type_check_only,
2121
)
22-
from typing_extensions import CapsuleType, Self, TypeAliasType, TypeVar, Unpack, deprecated
22+
from typing_extensions import Buffer, CapsuleType, Self, TypeAliasType, TypeVar, Unpack, deprecated
2323

2424
import numpy as np
2525
import numpy.typing as npt
@@ -30,7 +30,6 @@ from numpy import ( # noqa: ICN003
3030
_ModeKind,
3131
_OrderCF,
3232
_OrderKACF,
33-
_SupportsBuffer,
3433
# NOTE: These implicitly re-exported ufuncs are defined in this ext-module at runtime
3534
absolute as absolute,
3635
add as add,
@@ -1165,23 +1164,23 @@ def fromiter(
11651164
#
11661165
@overload
11671166
def frombuffer(
1168-
buffer: _SupportsBuffer,
1167+
buffer: Buffer,
11691168
*,
11701169
count: SupportsIndex = -1,
11711170
offset: SupportsIndex = 0,
11721171
**kwargs: Unpack[_KwargsL],
11731172
) -> _Array[np.float64]: ...
11741173
@overload
11751174
def frombuffer(
1176-
buffer: _SupportsBuffer,
1175+
buffer: Buffer,
11771176
dtype: _DTypeLike[_ScalarT],
11781177
count: SupportsIndex = -1,
11791178
offset: SupportsIndex = 0,
11801179
**kwargs: Unpack[_KwargsL],
11811180
) -> _Array[_ScalarT]: ...
11821181
@overload
11831182
def frombuffer(
1184-
buffer: _SupportsBuffer,
1183+
buffer: Buffer,
11851184
dtype: npt.DTypeLike,
11861185
count: SupportsIndex = -1,
11871186
offset: SupportsIndex = 0,

‎src/numpy-stubs/_core/defchararray.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from _typeshed import ConvertibleToInt
22
from typing import Any, Literal as L, SupportsIndex, TypeAlias, overload
3-
from typing_extensions import Never, Self, TypeVar, override
3+
from typing_extensions import Buffer, Never, Self, TypeVar, override
44

55
import numpy as np
6-
from numpy import _OrderKACF as _Order, _SortKind, _SupportsBuffer # noqa: ICN003
6+
from numpy import _OrderKACF as _Order, _SortKind # noqa: ICN003
77
from numpy._typing import (
88
NDArray,
99
_ArrayLikeAnyString_co as _ToAnyCharND,
@@ -158,7 +158,7 @@ class chararray(np.ndarray[_ShapeT_co, _DTypeT_co]):
158158
shape: _ToShape,
159159
itemsize: ConvertibleToInt = 1,
160160
unicode: L[False] = False,
161-
buffer: _SupportsBuffer | None = None,
161+
buffer: Buffer | None = None,
162162
offset: SupportsIndex = 0,
163163
strides: _ToShape | None = None,
164164
order: _Order = "C",
@@ -169,7 +169,7 @@ class chararray(np.ndarray[_ShapeT_co, _DTypeT_co]):
169169
shape: _ToShape,
170170
itemsize: ConvertibleToInt,
171171
unicode: L[True],
172-
buffer: _SupportsBuffer | None = None,
172+
buffer: Buffer | None = None,
173173
offset: SupportsIndex = 0,
174174
strides: _ToShape | None = None,
175175
order: _Order = "C",
@@ -181,7 +181,7 @@ class chararray(np.ndarray[_ShapeT_co, _DTypeT_co]):
181181
itemsize: ConvertibleToInt = 1,
182182
*,
183183
unicode: L[True],
184-
buffer: _SupportsBuffer | None = None,
184+
buffer: Buffer | None = None,
185185
offset: SupportsIndex = 0,
186186
strides: _ToShape | None = None,
187187
order: _Order = "C",

‎src/numpy-stubs/_core/records.pyi

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from _typeshed import StrOrBytesPath
22
from collections.abc import Iterable, Sequence
33
from typing import IO, Any, ClassVar, Literal as L, Protocol, SupportsIndex, TypeAlias, overload, type_check_only
4-
from typing_extensions import TypeVar
4+
from typing_extensions import Buffer, TypeVar
55

66
import numpy as np
7-
from numpy import _ByteOrder, _OrderKACF, _SupportsBuffer # noqa: ICN003
7+
from numpy import _ByteOrder, _OrderKACF # noqa: ICN003
88
from numpy._typing import ArrayLike, DTypeLike, NDArray, _ArrayLikeVoid_co, _NestedSequence, _ShapeLike
99

1010
__all__ = [
@@ -66,7 +66,7 @@ class recarray(np.ndarray[_ShapeT_co, _DTypeT_co]):
6666
subtype,
6767
shape: _ShapeLike,
6868
dtype: None = None,
69-
buf: _SupportsBuffer | None = None,
69+
buf: Buffer | None = None,
7070
offset: SupportsIndex = 0,
7171
strides: _ShapeLike | None = None,
7272
*,
@@ -82,7 +82,7 @@ class recarray(np.ndarray[_ShapeT_co, _DTypeT_co]):
8282
subtype,
8383
shape: _ShapeLike,
8484
dtype: DTypeLike,
85-
buf: _SupportsBuffer | None = None,
85+
buf: Buffer | None = None,
8686
offset: SupportsIndex = ...,
8787
strides: _ShapeLike | None = None,
8888
formats: None = None,
@@ -158,7 +158,7 @@ def fromrecords(
158158
# exported in `numpy.rec`
159159
@overload
160160
def fromstring(
161-
datastring: _SupportsBuffer,
161+
datastring: Buffer,
162162
dtype: DTypeLike,
163163
shape: _ShapeLike | None = None,
164164
offset: int = 0,
@@ -170,7 +170,7 @@ def fromstring(
170170
) -> _RecArray[record]: ...
171171
@overload
172172
def fromstring(
173-
datastring: _SupportsBuffer,
173+
datastring: Buffer,
174174
dtype: None = None,
175175
shape: _ShapeLike | None = None,
176176
offset: int = 0,
@@ -214,7 +214,7 @@ def fromfile(
214214

215215
# exported in `numpy.rec`
216216
@overload
217-
def array(
217+
def array( # type: ignore[overload-overlap]
218218
obj: _SCT | NDArray[_SCT],
219219
dtype: None = None,
220220
shape: _ShapeLike | None = None,

0 commit comments

Comments
 (0)