Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ENH: Virtually implement __buffer__ for Python < 3.12 #26784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions numpy/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1388,15 +1388,7 @@ _NumberType = TypeVar("_NumberType", bound=number[Any])
if sys.version_info >= (3, 12):
from collections.abc import Buffer as _SupportsBuffer
else:
_SupportsBuffer = (
bytes
| bytearray
| memoryview
| _array.array[Any]
| mmap.mmap
| NDArray[Any]
| generic
)
from typing_extensions import Buffer as _SupportsBuffer

_T = TypeVar("_T")
_T_co = TypeVar("_T_co", covariant=True)
Expand Down Expand Up @@ -1459,8 +1451,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]):
order: _OrderKACF = ...,
) -> _ArraySelf: ...

if sys.version_info >= (3, 12):
def __buffer__(self, flags: int, /) -> memoryview: ...
def __buffer__(self, flags: int, /) -> memoryview: ...

def __class_getitem__(self, item: Any) -> GenericAlias: ...

Expand Down Expand Up @@ -2622,8 +2613,7 @@ class generic(_ArrayOrScalarCommon):
@property
def flat(self: _ScalarType) -> flatiter[NDArray[_ScalarType]]: ...

if sys.version_info >= (3, 12):
def __buffer__(self, flags: int, /) -> memoryview: ...
def __buffer__(self, flags: int, /) -> memoryview: ...

@overload
def astype(
Expand Down
15 changes: 6 additions & 9 deletions numpy/_typing/_array_like.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,13 @@ def __array_function__(

if sys.version_info >= (3, 12):
from collections.abc import Buffer

ArrayLike = Buffer | _DualArrayLike[
dtype[Any],
Union[bool, int, float, complex, str, bytes],
]
else:
ArrayLike = _DualArrayLike[
dtype[Any],
Union[bool, int, float, complex, str, bytes],
]
from typing_extensions import Buffer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typing_extensions isn't a required dependency; it can only be used in stubs or if typing.TYPE_CHECKING


ArrayLike = Buffer | _DualArrayLike[
dtype[Any],
Union[bool, int, float, complex, str, bytes],
]

# `ArrayLike<X>_co`: array-like objects that can be coerced into `X`
# given the casting rules `same_kind`
Expand Down
9 changes: 4 additions & 5 deletions numpy/typing/tests/data/reveal/array_constructors.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,9 @@ assert_type(np.stack([A, A], out=B), SubClass[np.float64])
assert_type(np.block([[A, A], [A, A]]), npt.NDArray[Any])
assert_type(np.block(C), npt.NDArray[Any])

if sys.version_info >= (3, 12):
from collections.abc import Buffer
from typing_extensions import Buffer

def create_array(obj: npt.ArrayLike) -> npt.NDArray[Any]: ...
def create_array(obj: npt.ArrayLike) -> npt.NDArray[Any]: ...

buffer: Buffer
assert_type(create_array(buffer), npt.NDArray[Any])
buffer: Buffer
assert_type(create_array(buffer), npt.NDArray[Any])