Skip to content

Commit 4637688

Browse files
committed
🎨 massively simplify ctypeslib.as_ctypes
1 parent 9f9a138 commit 4637688

1 file changed

Lines changed: 14 additions & 53 deletions

File tree

‎src/numpy-stubs/ctypeslib.pyi‎

Lines changed: 14 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import _ctypes as _ct
22
import ctypes as ct
33
from _typeshed import StrOrBytesPath
44
from collections.abc import Iterable, Sequence
5-
from typing import Any, ClassVar, Generic, Literal as L, TypeAlias, overload
5+
from typing import Any, ClassVar, Generic, Literal as L, Protocol, TypeAlias, overload, type_check_only
66
from typing_extensions import TypeVar
77

88
import _numtype as _nt
@@ -35,6 +35,16 @@ _FlagsKind: TypeAlias = L[
3535

3636
###
3737

38+
_CT = TypeVar("_CT", bound=ct._CData)
39+
_CT_co = TypeVar("_CT_co", bound=ct._CData, covariant=True)
40+
41+
@type_check_only
42+
class _HasCType(Protocol[_CT_co]):
43+
@property
44+
def __ctype__(self, /) -> _CT_co: ...
45+
46+
###
47+
3848
c_intp = ct.c_ssize_t
3949

4050
class _ndptr(ct.c_void_p, Generic[_DTypeT0_co, _ShapeT0_co]):
@@ -106,59 +116,10 @@ def as_array(obj: _ArrayLike[_ScalarT], shape: _ShapeLike | None = ...) -> _nt.A
106116
@overload
107117
def as_array(obj: object, shape: _ShapeLike | None = ...) -> _nt.Array[Any]: ...
108118

109-
# NOTE: The overlapping overloads ignores are a temporary workaround for the non-unique
110-
# intp/long/longlong issues, and otherwise mypy false positives.
111-
# NOTE: `as_ctypes` doesn't support `void` values at the moment
112-
@overload # bool
113-
def as_ctypes(obj: np.bool) -> ct.c_bool: ...
114-
@overload
115-
def as_ctypes(obj: _nt.Array[np.bool]) -> ct.Array[ct.c_bool]: ...
116-
@overload # int8 / byte
117-
def as_ctypes(obj: np.int8) -> ct.c_int8: ...
118-
@overload
119-
def as_ctypes(obj: _nt.Array[np.int8]) -> ct.Array[ct.c_int8]: ...
120-
@overload # int16 / short
121-
def as_ctypes(obj: np.int16) -> ct.c_int16: ...
122-
@overload
123-
def as_ctypes(obj: _nt.Array[np.int16]) -> ct.Array[ct.c_int16]: ...
124-
@overload # int32 / int[c]
125-
def as_ctypes(obj: np.int32) -> ct.c_int32: ...
126-
@overload
127-
def as_ctypes(obj: _nt.Array[np.int32]) -> ct.Array[ct.c_int32]: ...
128-
@overload # int64 / longlong (which might be an alias for for `long`)
129-
def as_ctypes(obj: np.int64) -> ct.c_int64: ...
130-
@overload
131-
def as_ctypes(obj: _nt.Array[np.int64]) -> ct.Array[ct.c_int64]: ...
132-
@overload # uint8 / ubyte
133-
def as_ctypes(obj: np.uint8) -> ct.c_uint8: ...
134-
@overload
135-
def as_ctypes(obj: _nt.Array[np.uint8]) -> ct.Array[ct.c_uint8]: ...
136-
@overload # uint16 / ushort
137-
def as_ctypes(obj: np.uint16) -> ct.c_uint16: ...
138-
@overload
139-
def as_ctypes(obj: _nt.Array[np.uint16]) -> ct.Array[ct.c_uint16]: ...
140-
@overload # uint32 / uint
141-
def as_ctypes(obj: np.uint32) -> ct.c_uint32: ...
142-
@overload
143-
def as_ctypes(obj: _nt.Array[np.uint32]) -> ct.Array[ct.c_uint32]: ...
144-
@overload # uint64 / ulonglong
145-
def as_ctypes(obj: np.uint64) -> ct.c_uint64: ...
146-
@overload
147-
def as_ctypes(obj: _nt.Array[np.uint64]) -> ct.Array[ct.c_uint64]: ...
148-
@overload # float32 / single / float
149-
def as_ctypes(obj: np.float32) -> ct.c_float: ...
150-
@overload
151-
def as_ctypes(obj: _nt.Array[np.float32]) -> ct.Array[ct.c_float]: ...
152-
@overload # float64 / double
153-
def as_ctypes(obj: np.float64) -> ct.c_double: ...
154-
@overload
155-
def as_ctypes(obj: _nt.Array[np.float64]) -> ct.Array[ct.c_double]: ...
156-
@overload # float96 / float128 / longdouble
157-
def as_ctypes(obj: np.longdouble) -> ct.c_longdouble: ...
158-
@overload
159-
def as_ctypes(obj: _nt.Array[np.longdouble]) -> ct.Array[ct.c_longdouble]: ...
119+
#
120+
def as_ctypes(obj: _HasCType[_CT]) -> _CT: ...
160121

161-
# TODO(jorenham): https://github.com/numpy/numtype/issues/522
122+
#
162123
@overload
163124
def as_ctypes_type(dtype: _nt.ToDTypeBool) -> type[ct.c_bool]: ...
164125
@overload

0 commit comments

Comments
 (0)