Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
ndarray_conversion.pyi84 linesDownload Raw Back to reveal
1from typing import Any, assert_type
2
3import numpy as np
4import numpy.typing as npt
5
6b1_0d: np.ndarray[tuple[()], np.dtype[np.bool]]
7u2_1d: np.ndarray[tuple[int], np.dtype[np.uint16]]
8i4_2d: np.ndarray[tuple[int, int], np.dtype[np.int32]]
9f8_3d: np.ndarray[tuple[int, int, int], np.dtype[np.float64]]
10cG_4d: np.ndarray[tuple[int, int, int, int], np.dtype[np.clongdouble]]
11i0_nd: npt.NDArray[np.int_]
12uncertain_dtype: np.int32 | np.float64 | np.str_
13
14# item
15assert_type(i0_nd.item(), int)
16assert_type(i0_nd.item(1), int)
17assert_type(i0_nd.item(0, 1), int)
18assert_type(i0_nd.item((0, 1)), int)
19
20assert_type(b1_0d.item(()), bool)
21assert_type(u2_1d.item((0,)), int)
22assert_type(i4_2d.item(-1, 2), int)
23assert_type(f8_3d.item(2, 1, -1), float)
24assert_type(cG_4d.item(-0xEd_fed_Deb_a_dead_bee), complex)  # c'mon Ed, we talked about this...
25
26# tolist
27assert_type(b1_0d.tolist(), bool)
28assert_type(u2_1d.tolist(), list[int])
29assert_type(i4_2d.tolist(), list[list[int]])
30assert_type(f8_3d.tolist(), list[list[list[float]]])
31assert_type(cG_4d.tolist(), Any)
32assert_type(i0_nd.tolist(), Any)
33
34# regression tests for numpy/numpy#27944
35any_dtype: np.ndarray[Any, Any]
36any_sctype: np.ndarray[Any, Any]
37assert_type(any_dtype.tolist(), Any)
38assert_type(any_sctype.tolist(), Any)
39
40# tobytes is pretty simple
41# tofile does not return a value
42# dump does not return a value
43# dumps is pretty simple
44
45# astype
46assert_type(i0_nd.astype("float"), npt.NDArray[Any])
47assert_type(i0_nd.astype(float), npt.NDArray[Any])
48assert_type(i0_nd.astype(np.float64), npt.NDArray[np.float64])
49assert_type(i0_nd.astype(np.float64, "K"), npt.NDArray[np.float64])
50assert_type(i0_nd.astype(np.float64, "K", "unsafe"), npt.NDArray[np.float64])
51assert_type(i0_nd.astype(np.float64, "K", "unsafe", True), npt.NDArray[np.float64])
52assert_type(i0_nd.astype(np.float64, "K", "unsafe", True, True), npt.NDArray[np.float64])
53
54assert_type(np.astype(i0_nd, np.float64), npt.NDArray[np.float64])
55
56assert_type(i4_2d.astype(np.uint16), np.ndarray[tuple[int, int], np.dtype[np.uint16]])
57assert_type(np.astype(i4_2d, np.uint16), np.ndarray[tuple[int, int], np.dtype[np.uint16]])
58assert_type(f8_3d.astype(np.int16), np.ndarray[tuple[int, int, int], np.dtype[np.int16]])
59assert_type(np.astype(f8_3d, np.int16), np.ndarray[tuple[int, int, int], np.dtype[np.int16]])
60assert_type(i4_2d.astype(uncertain_dtype), np.ndarray[tuple[int, int], np.dtype[np.generic]])
61assert_type(np.astype(i4_2d, uncertain_dtype), np.ndarray[tuple[int, int], np.dtype])
62
63# byteswap
64assert_type(i0_nd.byteswap(), npt.NDArray[np.int_])
65assert_type(i0_nd.byteswap(True), npt.NDArray[np.int_])
66
67# copy
68assert_type(i0_nd.copy(), npt.NDArray[np.int_])
69assert_type(i0_nd.copy("C"), npt.NDArray[np.int_])
70
71assert_type(i0_nd.view(), npt.NDArray[np.int_])
72assert_type(i0_nd.view(np.float64), npt.NDArray[np.float64])
73assert_type(i0_nd.view(float), npt.NDArray[Any])
74assert_type(i0_nd.view(np.float64, np.matrix), np.matrix)
75
76# getfield
77assert_type(i0_nd.getfield("float"), npt.NDArray[Any])
78assert_type(i0_nd.getfield(float), npt.NDArray[Any])
79assert_type(i0_nd.getfield(np.float64), npt.NDArray[np.float64])
80assert_type(i0_nd.getfield(np.float64, 8), npt.NDArray[np.float64])
81
82# setflags does not return a value
83# fill does not return a value
84 
codekingpro/portable-devtools · Team Ai