diff options
author | Bas van Beek <b.f.van.beek@vu.nl> | 2021-10-29 17:35:13 +0200 |
---|---|---|
committer | Bas van Beek <b.f.van.beek@vu.nl> | 2021-10-29 17:41:15 +0200 |
commit | aac516a50b64bd1b25d9d5216c89de10b4baf61f (patch) | |
tree | c5fedd4a0ee0ffbcf35bdc7269e3f3fd944c564c | |
parent | a2b4bb7b67651d91a7bc9831c0a053800b658293 (diff) | |
download | numpy-aac516a50b64bd1b25d9d5216c89de10b4baf61f.tar.gz |
TST: Account for the platform-specific mapping of numpy- to ctypes-types
-rw-r--r-- | numpy/typing/tests/data/reveal/ctypeslib.pyi | 78 | ||||
-rw-r--r-- | numpy/typing/tests/test_typing.py | 33 |
2 files changed, 74 insertions, 37 deletions
diff --git a/numpy/typing/tests/data/reveal/ctypeslib.pyi b/numpy/typing/tests/data/reveal/ctypeslib.pyi index ae0b6b345..461a447d9 100644 --- a/numpy/typing/tests/data/reveal/ctypeslib.pyi +++ b/numpy/typing/tests/data/reveal/ctypeslib.pyi @@ -17,6 +17,7 @@ AR_int: npt.NDArray[np.int_] AR_longlong: npt.NDArray[np.longlong] AR_single: npt.NDArray[np.single] AR_double: npt.NDArray[np.double] +AR_longdouble: npt.NDArray[np.longdouble] AR_void: npt.NDArray[np.void] pointer: ctypes.pointer[Any] @@ -31,51 +32,54 @@ reveal_type(np.ctypeslib.ndpointer(np.int64, shape=(10, 3))) # E: Type[numpy.ct reveal_type(np.ctypeslib.ndpointer(int, shape=(1,))) # E: Type[numpy.ctypeslib._concrete_ndptr[numpy.dtype[Any]]] reveal_type(np.ctypeslib.as_ctypes_type(np.bool_)) # E: Type[ctypes.c_bool] -reveal_type(np.ctypeslib.as_ctypes_type(np.ubyte)) # E: Type[ctypes.c_ubyte] -reveal_type(np.ctypeslib.as_ctypes_type(np.ushort)) # E: Type[ctypes.c_ushort] -reveal_type(np.ctypeslib.as_ctypes_type(np.uintc)) # E: Type[ctypes.c_uint] -reveal_type(np.ctypeslib.as_ctypes_type(np.uint)) # E: Type[ctypes.c_ulong] -reveal_type(np.ctypeslib.as_ctypes_type(np.ulonglong)) # E: Type[ctypes.c_ulong] -reveal_type(np.ctypeslib.as_ctypes_type(np.byte)) # E: Type[ctypes.c_byte] -reveal_type(np.ctypeslib.as_ctypes_type(np.short)) # E: Type[ctypes.c_short] -reveal_type(np.ctypeslib.as_ctypes_type(np.intc)) # E: Type[ctypes.c_int] -reveal_type(np.ctypeslib.as_ctypes_type(np.int_)) # E: Type[ctypes.c_long] -reveal_type(np.ctypeslib.as_ctypes_type(np.longlong)) # E: Type[ctypes.c_long] -reveal_type(np.ctypeslib.as_ctypes_type(np.single)) # E: Type[ctypes.c_float] -reveal_type(np.ctypeslib.as_ctypes_type(np.double)) # E: Type[ctypes.c_double] -reveal_type(np.ctypeslib.as_ctypes_type(ctypes.c_double)) # E: Type[ctypes.c_double] +reveal_type(np.ctypeslib.as_ctypes_type(np.ubyte)) # E: Type[{c_ubyte}] +reveal_type(np.ctypeslib.as_ctypes_type(np.ushort)) # E: Type[{c_ushort}] +reveal_type(np.ctypeslib.as_ctypes_type(np.uintc)) # E: Type[{c_uint}] +reveal_type(np.ctypeslib.as_ctypes_type(np.uint)) # E: Type[{c_ulong}] +reveal_type(np.ctypeslib.as_ctypes_type(np.ulonglong)) # E: Type[{c_ulonglong}] +reveal_type(np.ctypeslib.as_ctypes_type(np.byte)) # E: Type[{c_byte}] +reveal_type(np.ctypeslib.as_ctypes_type(np.short)) # E: Type[{c_short}] +reveal_type(np.ctypeslib.as_ctypes_type(np.intc)) # E: Type[{c_int}] +reveal_type(np.ctypeslib.as_ctypes_type(np.int_)) # E: Type[{c_long}] +reveal_type(np.ctypeslib.as_ctypes_type(np.longlong)) # E: Type[{c_longlong}] +reveal_type(np.ctypeslib.as_ctypes_type(np.single)) # E: Type[{c_float}] +reveal_type(np.ctypeslib.as_ctypes_type(np.double)) # E: Type[{c_double}] +reveal_type(np.ctypeslib.as_ctypes_type(np.longdouble)) # E: Type[{c_longdouble}] +reveal_type(np.ctypeslib.as_ctypes_type(ctypes.c_double)) # E: Type[{c_double}] reveal_type(np.ctypeslib.as_ctypes_type("q")) # E: Type[ctypes.c_longlong] reveal_type(np.ctypeslib.as_ctypes_type([("i8", np.int64), ("f8", np.float64)])) # E: Type[Any] reveal_type(np.ctypeslib.as_ctypes_type("i8")) # E: Type[Any] reveal_type(np.ctypeslib.as_ctypes_type("f8")) # E: Type[Any] reveal_type(np.ctypeslib.as_ctypes(AR_bool.take(0))) # E: ctypes.c_bool -reveal_type(np.ctypeslib.as_ctypes(AR_ubyte.take(0))) # E: ctypes.c_ubyte -reveal_type(np.ctypeslib.as_ctypes(AR_ushort.take(0))) # E: ctypes.c_ushort -reveal_type(np.ctypeslib.as_ctypes(AR_uintc.take(0))) # E: ctypes.c_uint -reveal_type(np.ctypeslib.as_ctypes(AR_uint.take(0))) # E: ctypes.c_ulong -reveal_type(np.ctypeslib.as_ctypes(AR_ulonglong.take(0))) # E: ctypes.c_ulong -reveal_type(np.ctypeslib.as_ctypes(AR_byte.take(0))) # E: ctypes.c_byte -reveal_type(np.ctypeslib.as_ctypes(AR_short.take(0))) # E: ctypes.c_short -reveal_type(np.ctypeslib.as_ctypes(AR_intc.take(0))) # E: ctypes.c_int -reveal_type(np.ctypeslib.as_ctypes(AR_int.take(0))) # E: ctypes.c_long -reveal_type(np.ctypeslib.as_ctypes(AR_longlong.take(0))) # E: ctypes.c_long -reveal_type(np.ctypeslib.as_ctypes(AR_single.take(0))) # E: ctypes.c_float -reveal_type(np.ctypeslib.as_ctypes(AR_double.take(0))) # E: ctypes.c_double +reveal_type(np.ctypeslib.as_ctypes(AR_ubyte.take(0))) # E: {c_ubyte} +reveal_type(np.ctypeslib.as_ctypes(AR_ushort.take(0))) # E: {c_ushort} +reveal_type(np.ctypeslib.as_ctypes(AR_uintc.take(0))) # E: {c_uint} +reveal_type(np.ctypeslib.as_ctypes(AR_uint.take(0))) # E: {c_ulong} +reveal_type(np.ctypeslib.as_ctypes(AR_ulonglong.take(0))) # E: {c_ulonglong} +reveal_type(np.ctypeslib.as_ctypes(AR_byte.take(0))) # E: {c_byte} +reveal_type(np.ctypeslib.as_ctypes(AR_short.take(0))) # E: {c_short} +reveal_type(np.ctypeslib.as_ctypes(AR_intc.take(0))) # E: {c_int} +reveal_type(np.ctypeslib.as_ctypes(AR_int.take(0))) # E: {c_long} +reveal_type(np.ctypeslib.as_ctypes(AR_longlong.take(0))) # E: {c_longlong} +reveal_type(np.ctypeslib.as_ctypes(AR_single.take(0))) # E: {c_float} +reveal_type(np.ctypeslib.as_ctypes(AR_double.take(0))) # E: {c_double} +reveal_type(np.ctypeslib.as_ctypes(AR_longdouble.take(0))) # E: {c_longdouble} reveal_type(np.ctypeslib.as_ctypes(AR_void.take(0))) # E: Any reveal_type(np.ctypeslib.as_ctypes(AR_bool)) # E: ctypes.Array[ctypes.c_bool] -reveal_type(np.ctypeslib.as_ctypes(AR_ubyte)) # E: ctypes.Array[ctypes.c_ubyte] -reveal_type(np.ctypeslib.as_ctypes(AR_ushort)) # E: ctypes.Array[ctypes.c_ushort] -reveal_type(np.ctypeslib.as_ctypes(AR_uintc)) # E: ctypes.Array[ctypes.c_uint] -reveal_type(np.ctypeslib.as_ctypes(AR_uint)) # E: ctypes.Array[ctypes.c_ulong] -reveal_type(np.ctypeslib.as_ctypes(AR_ulonglong)) # E: ctypes.Array[ctypes.c_ulong] -reveal_type(np.ctypeslib.as_ctypes(AR_byte)) # E: ctypes.Array[ctypes.c_byte] -reveal_type(np.ctypeslib.as_ctypes(AR_short)) # E: ctypes.Array[ctypes.c_short] -reveal_type(np.ctypeslib.as_ctypes(AR_intc)) # E: ctypes.Array[ctypes.c_int] -reveal_type(np.ctypeslib.as_ctypes(AR_int)) # E: ctypes.Array[ctypes.c_long] -reveal_type(np.ctypeslib.as_ctypes(AR_longlong)) # E: ctypes.Array[ctypes.c_long] -reveal_type(np.ctypeslib.as_ctypes(AR_single)) # E: ctypes.Array[ctypes.c_float] -reveal_type(np.ctypeslib.as_ctypes(AR_double)) # E: ctypes.Array[ctypes.c_double] +reveal_type(np.ctypeslib.as_ctypes(AR_ubyte)) # E: ctypes.Array[{c_ubyte}] +reveal_type(np.ctypeslib.as_ctypes(AR_ushort)) # E: ctypes.Array[{c_ushort}] +reveal_type(np.ctypeslib.as_ctypes(AR_uintc)) # E: ctypes.Array[{c_uint}] +reveal_type(np.ctypeslib.as_ctypes(AR_uint)) # E: ctypes.Array[{c_ulong}] +reveal_type(np.ctypeslib.as_ctypes(AR_ulonglong)) # E: ctypes.Array[{c_ulonglong}] +reveal_type(np.ctypeslib.as_ctypes(AR_byte)) # E: ctypes.Array[{c_byte}] +reveal_type(np.ctypeslib.as_ctypes(AR_short)) # E: ctypes.Array[{c_short}] +reveal_type(np.ctypeslib.as_ctypes(AR_intc)) # E: ctypes.Array[{c_int}] +reveal_type(np.ctypeslib.as_ctypes(AR_int)) # E: ctypes.Array[{c_long}] +reveal_type(np.ctypeslib.as_ctypes(AR_longlong)) # E: ctypes.Array[{c_longlong}] +reveal_type(np.ctypeslib.as_ctypes(AR_single)) # E: ctypes.Array[{c_float}] +reveal_type(np.ctypeslib.as_ctypes(AR_double)) # E: ctypes.Array[{c_double}] +reveal_type(np.ctypeslib.as_ctypes(AR_longdouble)) # E: ctypes.Array[{c_longdouble}] reveal_type(np.ctypeslib.as_ctypes(AR_void)) # E: ctypes.Array[Any] reveal_type(np.ctypeslib.as_array(AR_ubyte)) # E: numpy.ndarray[Any, numpy.dtype[{ubyte}]] diff --git a/numpy/typing/tests/test_typing.py b/numpy/typing/tests/test_typing.py index 71e459df6..e0bcacd06 100644 --- a/numpy/typing/tests/test_typing.py +++ b/numpy/typing/tests/test_typing.py @@ -187,6 +187,38 @@ def _test_fail( raise AssertionError(_FAIL_MSG2.format(lineno, expression, expected_error, error)) +def _construct_ctypes_dict() -> dict[str, str]: + dct = { + "ubyte": "c_ubyte", + "ushort": "c_ushort", + "uintc": "c_uint", + "uint": "c_ulong", + "ulonglong": "c_ulonglong", + "byte": "c_byte", + "short": "c_short", + "intc": "c_int", + "int_": "c_long", + "longlong": "c_longlong", + "single": "c_float", + "double": "c_double", + "longdouble": "c_longdouble", + } + + # Match `ctypes` names to the first ctypes type with a given kind and + # precision, e.g. {"c_double": "c_double", "c_longdouble": "c_double"} + # if both types represent 64-bit floats. + # In this context "first" is defined by the order of `dct` + ret = {} + visited: dict[tuple[str, int], str] = {} + for np_name, ct_name in dct.items(): + np_scalar = getattr(np, np_name)() + + # Find the first `ctypes` type for a given `kind`/`itemsize` combo + key = (np_scalar.dtype.kind, np_scalar.dtype.itemsize) + ret[ct_name] = visited.setdefault(key, f"ctypes.{ct_name}") + return ret + + def _construct_format_dict() -> dict[str, str]: dct = {k.split(".")[-1]: v.replace("numpy", "numpy.typing") for k, v in _PRECISION_DICT.items()} @@ -261,6 +293,7 @@ def _construct_format_dict() -> dict[str, str]: #: A dictionary with all supported format keys (as keys) #: and matching values FORMAT_DICT: dict[str, str] = _construct_format_dict() +FORMAT_DICT.update(_construct_ctypes_dict()) def _parse_reveals(file: IO[str]) -> tuple[npt.NDArray[np.str_], list[str]]: |