1
1
import ctypes as ct
2
- import datetime as dt
3
2
from decimal import Decimal
4
3
from fractions import Fraction
5
- from typing import Any , Literal , TypeAlias
6
4
from typing_extensions import LiteralString , assert_type
7
5
8
6
import numpy as np
9
7
from numpy .dtypes import StringDType
10
8
11
- # a combination of likely `object` dtype-like candidates (no `_co`)
12
- _PyObjectLike : TypeAlias = Decimal | Fraction | dt .datetime | dt .timedelta
13
-
14
9
dtype_U : np .dtype [np .str_ ]
15
10
dtype_V : np .dtype [np .void ]
16
11
dtype_i8 : np .dtype [np .int64 ]
17
12
18
- py_int_co : type [int | bool ]
19
- py_float_co : type [float | int | bool ]
20
- py_complex_co : type [complex | float | int | bool ]
21
- py_object : type [ _PyObjectLike ]
13
+ py_int_co : type [int ]
14
+ py_float_co : type [float ]
15
+ py_complex_co : type [complex ]
16
+ py_object : type
22
17
py_character : type [str | bytes ]
23
18
py_flexible : type [str | bytes | memoryview ]
24
19
25
20
ct_floating : type [ct .c_float | ct .c_double | ct .c_longdouble ]
26
21
ct_number : type [ct .c_uint8 | ct .c_float ]
27
22
ct_generic : type [ct .c_bool | ct .c_char ]
28
23
29
- cs_integer : Literal ["u1" , "<i2" , "L" ]
30
- cs_number : Literal ["=L" , "i" , "c16" ]
31
- cs_flex : Literal [">V" , "S" ]
32
- cs_generic : Literal ["H" , "U" , "h" , "|M8[Y]" , "?" ]
33
-
34
- dt_inexact : np .dtype [np .inexact ]
35
24
dt_string : StringDType
36
25
37
26
assert_type (np .dtype (np .float64 ), np .dtype [np .float64 ])
38
27
assert_type (np .dtype (np .float64 , metadata = {"test" : "test" }), np .dtype [np .float64 ])
39
28
assert_type (np .dtype (np .int64 ), np .dtype [np .int64 ])
40
29
41
30
# String aliases
42
- assert_type (np .dtype ("float64" ), np .dtype [np .float64 ])
43
- assert_type (np .dtype ("float32" ), np .dtype [np .float32 ])
44
- assert_type (np .dtype ("int64" ), np .dtype [np .int64 ])
45
- assert_type (np .dtype ("int32" ), np .dtype [np .int32 ])
46
31
assert_type (np .dtype ("bool" ), np .dtype [np .bool ])
32
+ assert_type (np .dtype ("int32" ), np .dtype [np .int32 ])
33
+ assert_type (np .dtype ("int64" ), np .dtype [np .int64 ])
34
+ assert_type (np .dtype ("float32" ), np .dtype [np .float32 ])
35
+ assert_type (np .dtype ("float64" ), np .dtype [np .float64 ])
47
36
assert_type (np .dtype ("bytes" ), np .dtype [np .bytes_ ])
48
37
assert_type (np .dtype ("str" ), np .dtype [np .str_ ])
49
38
50
39
# Python types
51
40
assert_type (np .dtype (bool ), np .dtype [np .bool ])
52
- assert_type (np .dtype (py_int_co ), np .dtype [np .int_ | np .bool ])
53
- assert_type (np .dtype (int ), np .dtype [np .int_ | np .bool ])
54
- assert_type (np .dtype (py_float_co ), np .dtype [np .float64 | np .int_ | np .bool ])
55
- assert_type (np .dtype (float ), np .dtype [np .float64 | np .int_ | np .bool ])
56
- assert_type (np .dtype (py_complex_co ), np .dtype [np .complex128 | np .float64 | np .int_ | np .bool ])
57
- assert_type (np .dtype (complex ), np .dtype [np .complex128 | np .float64 | np .int_ | np .bool ])
58
- assert_type (np .dtype (py_object ), np .dtype [np .object_ | Any ])
41
+ assert_type (np .dtype (int ), np .dtype [np .int_ ])
42
+ assert_type (np .dtype (float ), np .dtype [np .float64 ])
43
+ assert_type (np .dtype (complex ), np .dtype [np .complex128 ])
44
+ assert_type (np .dtype (object ), np .dtype [np .object_ ])
59
45
assert_type (np .dtype (str ), np .dtype [np .str_ ])
60
46
assert_type (np .dtype (bytes ), np .dtype [np .bytes_ ])
61
- assert_type (np .dtype (py_character ), np .dtype [np .character ])
62
47
assert_type (np .dtype (memoryview ), np .dtype [np .void ])
63
- assert_type (np .dtype (py_flexible ), np .dtype [np .flexible ])
48
+
49
+ assert_type (np .dtype (np .signedinteger ), np .dtype [np .signedinteger ])
50
+ assert_type (np .dtype (np .unsignedinteger ), np .dtype [np .unsignedinteger ])
51
+ assert_type (np .dtype (np .integer ), np .dtype [np .integer ])
52
+ assert_type (np .dtype (np .floating ), np .dtype [np .floating ])
53
+ assert_type (np .dtype (np .complexfloating ), np .dtype [np .complexfloating ])
54
+ assert_type (np .dtype (np .inexact ), np .dtype [np .inexact ])
55
+ assert_type (np .dtype (np .number ), np .dtype [np .number ])
56
+ # NOTE: `character` and `flexible` always fail on mypy because of some mypy bug
57
+ assert_type (np .dtype (np .generic ), np .dtype [np .generic ])
64
58
65
59
assert_type (np .dtype (Decimal ), np .dtype [np .object_ ])
66
60
assert_type (np .dtype (Fraction ), np .dtype [np .object_ ])
@@ -70,25 +64,22 @@ assert_type(np.dtype("u1"), np.dtype[np.uint8])
70
64
assert_type (np .dtype ("int_" ), np .dtype [np .intp ])
71
65
assert_type (np .dtype ("longlong" ), np .dtype [np .longlong ])
72
66
assert_type (np .dtype (">g" ), np .dtype [np .longdouble ])
73
- assert_type (np .dtype (cs_integer ), np .dtype [np .integer ])
74
- assert_type (np .dtype (cs_number ), np .dtype [np .number ])
75
- assert_type (np .dtype (cs_flex ), np .dtype [np .flexible ])
76
- assert_type (np .dtype (cs_generic ), np .dtype [np .generic ])
77
67
78
68
# ctypes
79
- assert_type (np .dtype (ct .c_double ), np .dtype [np .float64 ])
80
- assert_type (np .dtype (ct .c_longlong ), np .dtype [np .longlong ])
81
- assert_type (np .dtype (ct .c_uint32 ), np .dtype [np .uint32 ])
82
69
assert_type (np .dtype (ct .c_bool ), np .dtype [np .bool ])
83
- assert_type (np .dtype (ct .c_char ), np .dtype [np .bytes_ ])
70
+ assert_type (np .dtype (ct .c_uint32 ), np .dtype [np .uint32 ])
71
+ assert_type (np .dtype (ct .c_ssize_t ), np .dtype [np .intp ])
72
+ assert_type (np .dtype (ct .c_longlong ), np .dtype [np .longlong ])
73
+ assert_type (np .dtype (ct .c_double ), np .dtype [np .double ])
84
74
assert_type (np .dtype (ct .py_object ), np .dtype [np .object_ ])
75
+ assert_type (np .dtype (ct .c_char ), np .dtype [np .bytes_ ])
85
76
86
77
# Special case for None
87
78
assert_type (np .dtype (None ), np .dtype [np .float64 ])
88
79
89
80
# Dypes of dtypes
90
81
assert_type (np .dtype (np .dtype (np .float64 )), np .dtype [np .float64 ])
91
- assert_type (np .dtype (dt_inexact ), np .dtype [np .inexact ])
82
+ assert_type (np .dtype (np . dtype ( np . inexact ) ), np .dtype [np .inexact ])
92
83
93
84
# Parameterized dtypes
94
85
assert_type (np .dtype ("S8" ), np .dtype )
0 commit comments