Skip to content

Commit

Permalink
TYP: Add annotations for the py3.12 buffer protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
BvB93 authored and charris committed Sep 14, 2023
1 parent 2e84b15 commit 5ef53c6
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions numpy/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import builtins
import sys
import os
import mmap
import ctypes as ct
Expand Down Expand Up @@ -1440,17 +1441,18 @@ _ShapeType = TypeVar("_ShapeType", bound=Any)
_ShapeType2 = TypeVar("_ShapeType2", bound=Any)
_NumberType = TypeVar("_NumberType", bound=number[Any])

# There is currently no exhaustive way to type the buffer protocol,
# as it is implemented exclusively in the C API (python/typing#593)
_SupportsBuffer = Union[
bytes,
bytearray,
memoryview,
_array.array[Any],
mmap.mmap,
NDArray[Any],
generic,
]
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
)

_T = TypeVar("_T")
_T_co = TypeVar("_T_co", covariant=True)
Expand Down Expand Up @@ -1513,6 +1515,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]):
order: _OrderKACF = ...,
) -> _ArraySelf: ...

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

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

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

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

@overload
def astype(
self,
Expand Down Expand Up @@ -2772,6 +2780,9 @@ class object_(generic):
def __float__(self) -> float: ...
def __complex__(self) -> complex: ...

if sys.version_info >= (3, 12):
def __release_buffer__(self, buffer: memoryview, /) -> None: ...

# The `datetime64` constructors requires an object with the three attributes below,
# and thus supports datetime duck typing
class _DatetimeScalar(Protocol):
Expand Down

0 comments on commit 5ef53c6

Please sign in to comment.