Skip to content

Commit b95cde0

Browse files
authored
Merge pull request #28515 from jorenham/numtype/227
TYP: stub ``numpy._core._dtype[_ctypes]``
2 parents 27bc1c4 + d6262f1 commit b95cde0

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed

numpy/_core/_dtype.pyi

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from typing import Any, Final, TypeAlias, TypedDict, overload, type_check_only
2+
from typing import Literal as L
3+
4+
from typing_extensions import ReadOnly, TypeVar
5+
6+
import numpy as np
7+
8+
###
9+
10+
_T = TypeVar("_T")
11+
12+
_Name: TypeAlias = L["uint", "int", "complex", "float", "bool", "void", "object", "datetime", "timedelta", "bytes", "str"]
13+
14+
@type_check_only
15+
class _KindToStemType(TypedDict):
16+
u: ReadOnly[L["uint"]]
17+
i: ReadOnly[L["int"]]
18+
c: ReadOnly[L["complex"]]
19+
f: ReadOnly[L["float"]]
20+
b: ReadOnly[L["bool"]]
21+
V: ReadOnly[L["void"]]
22+
O: ReadOnly[L["object"]]
23+
M: ReadOnly[L["datetime"]]
24+
m: ReadOnly[L["timedelta"]]
25+
S: ReadOnly[L["bytes"]]
26+
U: ReadOnly[L["str"]]
27+
28+
###
29+
30+
_kind_to_stem: Final[_KindToStemType] = ...
31+
32+
#
33+
def _kind_name(dtype: np.dtype[Any]) -> _Name: ...
34+
def __str__(dtype: np.dtype[Any]) -> str: ...
35+
def __repr__(dtype: np.dtype[Any]) -> str: ...
36+
37+
#
38+
def _isunsized(dtype: np.dtype[Any]) -> bool: ...
39+
def _is_packed(dtype: np.dtype[Any]) -> bool: ...
40+
def _name_includes_bit_suffix(dtype: np.dtype[Any]) -> bool: ...
41+
42+
#
43+
def _construction_repr(dtype: np.dtype[Any], include_align: bool = False, short: bool = False) -> str: ...
44+
def _scalar_str(dtype: np.dtype[Any], short: bool) -> str: ...
45+
def _byte_order_str(dtype: np.dtype[Any]) -> str: ...
46+
def _datetime_metadata_str(dtype: np.dtype[Any]) -> str: ...
47+
def _struct_dict_str(dtype: np.dtype[Any], includealignedflag: bool) -> str: ...
48+
def _struct_list_str(dtype: np.dtype[Any]) -> str: ...
49+
def _struct_str(dtype: np.dtype[Any], include_align: bool) -> str: ...
50+
def _subarray_str(dtype: np.dtype[Any]) -> str: ...
51+
def _name_get(dtype: np.dtype[Any]) -> str: ...
52+
53+
#
54+
@overload
55+
def _unpack_field(dtype: np.dtype[Any], offset: int, title: _T) -> tuple[np.dtype[Any], int, _T]: ...
56+
@overload
57+
def _unpack_field(dtype: np.dtype[Any], offset: int, title: None = None) -> tuple[np.dtype[Any], int, None]: ...
58+
def _aligned_offset(offset: int, alignment: int) -> int: ...

numpy/_core/_dtype_ctypes.pyi

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import _ctypes
2+
import ctypes as ct
3+
from typing import Any, overload
4+
5+
import numpy as np
6+
7+
#
8+
@overload
9+
def dtype_from_ctypes_type(t: type[_ctypes.Array[Any] | _ctypes.Structure]) -> np.dtype[np.void]: ...
10+
@overload
11+
def dtype_from_ctypes_type(t: type[ct.c_bool]) -> np.dtype[np.bool]: ...
12+
@overload
13+
def dtype_from_ctypes_type(t: type[ct.c_int8 | ct.c_byte]) -> np.dtype[np.int8]: ...
14+
@overload
15+
def dtype_from_ctypes_type(t: type[ct.c_uint8 | ct.c_ubyte]) -> np.dtype[np.uint8]: ...
16+
@overload
17+
def dtype_from_ctypes_type(t: type[ct.c_int16 | ct.c_short]) -> np.dtype[np.int16]: ...
18+
@overload
19+
def dtype_from_ctypes_type(t: type[ct.c_uint16 | ct.c_ushort]) -> np.dtype[np.uint16]: ...
20+
@overload
21+
def dtype_from_ctypes_type(t: type[ct.c_int32 | ct.c_int]) -> np.dtype[np.int32]: ...
22+
@overload
23+
def dtype_from_ctypes_type(t: type[ct.c_uint32 | ct.c_uint]) -> np.dtype[np.uint32]: ...
24+
@overload
25+
def dtype_from_ctypes_type(t: type[ct.c_ssize_t | ct.c_long]) -> np.dtype[np.int32 | np.int64]: ...
26+
@overload
27+
def dtype_from_ctypes_type(t: type[ct.c_size_t | ct.c_ulong]) -> np.dtype[np.uint32 | np.uint64]: ...
28+
@overload
29+
def dtype_from_ctypes_type(t: type[ct.c_int64 | ct.c_longlong]) -> np.dtype[np.int64]: ...
30+
@overload
31+
def dtype_from_ctypes_type(t: type[ct.c_uint64 | ct.c_ulonglong]) -> np.dtype[np.uint64]: ...
32+
@overload
33+
def dtype_from_ctypes_type(t: type[ct.c_float]) -> np.dtype[np.float32]: ...
34+
@overload
35+
def dtype_from_ctypes_type(t: type[ct.c_double]) -> np.dtype[np.float64]: ...
36+
@overload
37+
def dtype_from_ctypes_type(t: type[ct.c_longdouble]) -> np.dtype[np.longdouble]: ...
38+
@overload
39+
def dtype_from_ctypes_type(t: type[ct.c_char]) -> np.dtype[np.bytes_]: ...
40+
@overload
41+
def dtype_from_ctypes_type(t: type[ct.py_object[Any]]) -> np.dtype[np.object_]: ...
42+
43+
# NOTE: the complex ctypes on python>=3.14 are not yet supported at runtim, see
44+
# https://github.com/numpy/numpy/issues/28360
45+
46+
#
47+
def _from_ctypes_array(t: type[_ctypes.Array[Any]]) -> np.dtype[np.void]: ...
48+
def _from_ctypes_structure(t: type[_ctypes.Structure]) -> np.dtype[np.void]: ...
49+
def _from_ctypes_union(t: type[_ctypes.Union]) -> np.dtype[np.void]: ...
50+
51+
# keep in sync with `dtype_from_ctypes_type` (minus the first overload)
52+
@overload
53+
def _from_ctypes_scalar(t: type[ct.c_bool]) -> np.dtype[np.bool]: ...
54+
@overload
55+
def _from_ctypes_scalar(t: type[ct.c_int8 | ct.c_byte]) -> np.dtype[np.int8]: ...
56+
@overload
57+
def _from_ctypes_scalar(t: type[ct.c_uint8 | ct.c_ubyte]) -> np.dtype[np.uint8]: ...
58+
@overload
59+
def _from_ctypes_scalar(t: type[ct.c_int16 | ct.c_short]) -> np.dtype[np.int16]: ...
60+
@overload
61+
def _from_ctypes_scalar(t: type[ct.c_uint16 | ct.c_ushort]) -> np.dtype[np.uint16]: ...
62+
@overload
63+
def _from_ctypes_scalar(t: type[ct.c_int32 | ct.c_int]) -> np.dtype[np.int32]: ...
64+
@overload
65+
def _from_ctypes_scalar(t: type[ct.c_uint32 | ct.c_uint]) -> np.dtype[np.uint32]: ...
66+
@overload
67+
def _from_ctypes_scalar(t: type[ct.c_ssize_t | ct.c_long]) -> np.dtype[np.int32 | np.int64]: ...
68+
@overload
69+
def _from_ctypes_scalar(t: type[ct.c_size_t | ct.c_ulong]) -> np.dtype[np.uint32 | np.uint64]: ...
70+
@overload
71+
def _from_ctypes_scalar(t: type[ct.c_int64 | ct.c_longlong]) -> np.dtype[np.int64]: ...
72+
@overload
73+
def _from_ctypes_scalar(t: type[ct.c_uint64 | ct.c_ulonglong]) -> np.dtype[np.uint64]: ...
74+
@overload
75+
def _from_ctypes_scalar(t: type[ct.c_float]) -> np.dtype[np.float32]: ...
76+
@overload
77+
def _from_ctypes_scalar(t: type[ct.c_double]) -> np.dtype[np.float64]: ...
78+
@overload
79+
def _from_ctypes_scalar(t: type[ct.c_longdouble]) -> np.dtype[np.longdouble]: ...
80+
@overload
81+
def _from_ctypes_scalar(t: type[ct.c_char]) -> np.dtype[np.bytes_]: ...
82+
@overload
83+
def _from_ctypes_scalar(t: type[ct.py_object[Any]]) -> np.dtype[np.object_]: ...

numpy/_core/meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,9 @@ python_sources = [
13411341
'_asarray.py',
13421342
'_asarray.pyi',
13431343
'_dtype.py',
1344+
'_dtype.pyi',
13441345
'_dtype_ctypes.py',
1346+
'_dtype_ctypes.pyi',
13451347
'_exceptions.py',
13461348
'_internal.py',
13471349
'_internal.pyi',

numpy/core/_dtype.pyi

Whitespace-only changes.

numpy/core/_dtype_ctypes.pyi

Whitespace-only changes.

0 commit comments

Comments
 (0)