Skip to content

Commit eca1190

Browse files
committed
🎨 simplify ctypeslib.as_ctypes_type using _numtype.ToDType*
1 parent 7521a86 commit eca1190

2 files changed

Lines changed: 15 additions & 44 deletions

File tree

‎src/numpy-stubs/@test/static/accept/ctypeslib.pyi‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,3 @@ assert_type(np.ctypeslib.as_ctypes(AR_f8), ct.Array[ct.c_double])
7878
assert_type(np.ctypeslib.as_array(AR_u1), npt.NDArray[np.ubyte])
7979
assert_type(np.ctypeslib.as_array(1), npt.NDArray[Any])
8080
assert_type(np.ctypeslib.as_array(pointer), npt.NDArray[Any])
81-
82-
assert_type(np.ctypeslib.as_ctypes_type(int), type[ct.c_ssize_t])
83-
assert_type(np.ctypeslib.as_ctypes_type("N"), type[ct.c_size_t])

‎src/numpy-stubs/ctypeslib.pyi‎

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,8 @@ import _numtype as _nt
99
import numpy as np
1010
from numpy._core._internal import _ctypes
1111
from numpy._core.multiarray import flagsobj
12-
from numpy._typing import _ArrayLike, _BoolCodes, _DTypeLike, _Shape, _ShapeLike, _VoidDTypeLike
13-
from numpy._typing._char_codes import (
14-
_Float32Codes,
15-
_Float64Codes,
16-
_Int8Codes,
17-
_Int16Codes,
18-
_Int32Codes,
19-
_Int64Codes,
20-
_IntPCodes,
21-
_LongCodes,
22-
_LongDoubleCodes,
23-
_UInt8Codes,
24-
_UInt16Codes,
25-
_UInt32Codes,
26-
_UInt64Codes,
27-
_UIntPCodes,
28-
_ULongCodes,
29-
)
12+
from numpy._typing import _ArrayLike, _DTypeLike, _Shape, _ShapeLike, _VoidDTypeLike
13+
from numpy._typing._char_codes import _LongCodes, _ULongCodes
3014

3115
__all__ = ["as_array", "as_ctypes", "as_ctypes_type", "c_intp", "load_library", "ndpointer"]
3216

@@ -176,43 +160,33 @@ def as_ctypes(obj: _nt.Array[np.longdouble]) -> ct.Array[ct.c_longdouble]: ...
176160

177161
# TODO(jorenham): https://github.com/numpy/numtype/issues/522
178162
@overload
179-
def as_ctypes_type(dtype: _DTypeLike[np.bool] | type[bool | ct.c_bool] | _BoolCodes) -> type[ct.c_bool]: ...
163+
def as_ctypes_type(dtype: _nt.ToDTypeBool) -> type[ct.c_bool]: ...
180164
@overload
181-
def as_ctypes_type(dtype: _DTypeLike[np.int8] | type[ct.c_int8 | ct.c_byte] | _Int8Codes) -> type[ct.c_int8]: ...
165+
def as_ctypes_type(dtype: _nt.ToDTypeInt8) -> type[ct.c_int8]: ...
182166
@overload
183-
def as_ctypes_type(dtype: _DTypeLike[np.int16] | type[ct.c_int16 | ct.c_short] | _Int16Codes) -> type[ct.c_int16]: ...
167+
def as_ctypes_type(dtype: _nt.ToDTypeUInt8) -> type[ct.c_uint8]: ...
184168
@overload
185-
def as_ctypes_type(dtype: _DTypeLike[np.int32] | type[ct.c_int32 | ct.c_int] | _Int32Codes) -> type[ct.c_int32]: ...
169+
def as_ctypes_type(dtype: _nt.ToDTypeInt16) -> type[ct.c_int16]: ...
186170
@overload
187-
def as_ctypes_type(dtype: type[ct.c_long] | _LongCodes) -> type[ct.c_long]: ...
171+
def as_ctypes_type(dtype: _nt.ToDTypeUInt16) -> type[ct.c_uint16]: ...
188172
@overload
189-
def as_ctypes_type(dtype: type[_nt.JustInt | ct.c_ssize_t] | _IntPCodes) -> type[ct.c_ssize_t]: ...
173+
def as_ctypes_type(dtype: _nt.ToDTypeInt32) -> type[ct.c_int32]: ...
190174
@overload
191-
def as_ctypes_type(dtype: _DTypeLike[np.int64] | type[ct.c_int64] | _Int64Codes) -> type[ct.c_int64]: ...
175+
def as_ctypes_type(dtype: _nt.ToDTypeUInt32) -> type[ct.c_uint32]: ...
192176
@overload
193-
def as_ctypes_type(dtype: _DTypeLike[np.uint8] | type[ct.c_uint8 | ct.c_ubyte] | _UInt8Codes) -> type[ct.c_uint8]: ...
177+
def as_ctypes_type(dtype: _nt.ToDTypeInt64) -> type[ct.c_int64]: ...
194178
@overload
195-
def as_ctypes_type(
196-
dtype: _DTypeLike[np.uint16] | type[ct.c_uint16 | ct.c_ushort] | _UInt16Codes,
197-
) -> type[ct.c_uint16]: ...
179+
def as_ctypes_type(dtype: _nt.ToDTypeUInt64) -> type[ct.c_uint64]: ...
198180
@overload
199-
def as_ctypes_type(
200-
dtype: _DTypeLike[np.uint32] | type[ct.c_uint32 | ct.c_uint] | _UInt32Codes,
201-
) -> type[ct.c_uint32]: ...
181+
def as_ctypes_type(dtype: type[ct.c_long] | _LongCodes) -> type[ct.c_long]: ...
202182
@overload
203183
def as_ctypes_type(dtype: type[ct.c_ulong] | _ULongCodes) -> type[ct.c_ulong]: ...
204184
@overload
205-
def as_ctypes_type(dtype: type[ct.c_void_p | ct.c_size_t] | _UIntPCodes) -> type[ct.c_size_t]: ...
206-
@overload
207-
def as_ctypes_type(dtype: _DTypeLike[np.uint64] | type[ct.c_uint64] | _UInt64Codes) -> type[ct.c_uint64]: ...
208-
@overload
209-
def as_ctypes_type(dtype: _DTypeLike[np.float32] | type[ct.c_float] | _Float32Codes) -> type[ct.c_float]: ...
185+
def as_ctypes_type(dtype: _nt.ToDTypeFloat32) -> type[ct.c_float]: ...
210186
@overload
211-
def as_ctypes_type(dtype: _DTypeLike[np.float64] | type[ct.c_double] | _Float64Codes) -> type[ct.c_double]: ...
187+
def as_ctypes_type(dtype: _nt.ToDTypeFloat64) -> type[ct.c_double]: ...
212188
@overload
213-
def as_ctypes_type(
214-
dtype: _DTypeLike[np.longdouble] | type[ct.c_longdouble] | _LongDoubleCodes,
215-
) -> type[ct.c_longdouble]: ...
189+
def as_ctypes_type(dtype: _nt.ToDTypeLongDouble) -> type[ct.c_longdouble]: ...
216190
@overload
217191
def as_ctypes_type(dtype: _VoidDTypeLike) -> _ct._UnionType | _ct._PyCStructType: ...
218192
@overload

0 commit comments

Comments
 (0)