-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathcheck-ctypes.test
More file actions
183 lines (152 loc) · 7.39 KB
/
check-ctypes.test
File metadata and controls
183 lines (152 loc) · 7.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
[case testCtypesArrayStandardElementType]
import ctypes
class MyCInt(ctypes.c_int):
pass
intarr4 = ctypes.c_int * 4
a = intarr4(1, ctypes.c_int(2), MyCInt(3), 4)
intarr4(1, 2, 3, "invalid") # E: Array constructor argument 4 of type "str" is not convertible to the array element type "c_int"
reveal_type(a) # N: Revealed type is "_ctypes.Array[ctypes.c_int]"
reveal_type(a[0]) # N: Revealed type is "builtins.int"
reveal_type(a[1:3]) # N: Revealed type is "builtins.list[builtins.int]"
a[0] = 42
a[1] = ctypes.c_int(42)
a[2] = MyCInt(42)
a[3] = b"bytes" # E: No overload variant of "__setitem__" of "Array" matches argument types "int", "bytes" \
# N: Possible overload variants: \
# N: def __setitem__(self, int, c_int | int, /) -> None \
# N: def __setitem__(self, slice, list[c_int | int], /) -> None
for x in a:
reveal_type(x) # N: Revealed type is "builtins.int"
[builtins fixtures/floatdict.pyi]
[typing fixtures/typing-medium.pyi]
[case testCtypesArrayCustomElementType]
import ctypes
from typing import Union, List
class MyCInt(ctypes.c_int):
pass
myintarr4 = MyCInt * 4
mya = myintarr4(1, 2, MyCInt(3), 4)
myintarr4(1, ctypes.c_int(2), MyCInt(3), "invalid") # E: Array constructor argument 2 of type "c_int" is not convertible to the array element type "MyCInt" \
# E: Array constructor argument 4 of type "str" is not convertible to the array element type "MyCInt"
reveal_type(mya) # N: Revealed type is "_ctypes.Array[__main__.MyCInt]"
reveal_type(mya[0]) # N: Revealed type is "__main__.MyCInt"
reveal_type(mya[1:3]) # N: Revealed type is "builtins.list[__main__.MyCInt]"
mya[0] = 42
mya[1] = ctypes.c_int(42) # E: No overload variant of "__setitem__" of "Array" matches argument types "int", "c_int" \
# N: Possible overload variants: \
# N: def __setitem__(self, int, MyCInt | int, /) -> None \
# N: def __setitem__(self, slice, list[MyCInt | int], /) -> None
mya[2] = MyCInt(42)
mya[3] = b"bytes" # E: No overload variant of "__setitem__" of "Array" matches argument types "int", "bytes" \
# N: Possible overload variants: \
# N: def __setitem__(self, int, MyCInt | int, /) -> None \
# N: def __setitem__(self, slice, list[MyCInt | int], /) -> None
for myx in mya:
reveal_type(myx) # N: Revealed type is "__main__.MyCInt"
myu: Union[ctypes.Array[ctypes.c_int], List[str]]
for myi in myu:
reveal_type(myi) # N: Revealed type is "builtins.int | builtins.str"
[builtins fixtures/floatdict.pyi]
[typing fixtures/typing-medium.pyi]
[case testCtypesArrayUnionElementType]
import ctypes
from typing import Union
class MyCInt(ctypes.c_int):
pass
mya: ctypes.Array[Union[MyCInt, ctypes.c_uint]]
reveal_type(mya) # N: Revealed type is "_ctypes.Array[__main__.MyCInt | ctypes.c_uint]"
reveal_type(mya[0]) # N: Revealed type is "__main__.MyCInt | builtins.int"
reveal_type(mya[1:3]) # N: Revealed type is "builtins.list[__main__.MyCInt | builtins.int]"
# The behavior here is not strictly correct, but intentional.
# See the comment in mypy.plugins.ctypes._autoconvertible_to_cdata for details.
mya[0] = 42
mya[1] = ctypes.c_uint(42)
mya[2] = MyCInt(42)
mya[3] = b"bytes" # E: No overload variant of "__setitem__" of "Array" matches argument types "int", "bytes" \
# N: Possible overload variants: \
# N: def __setitem__(self, int, MyCInt | int | c_uint, /) -> None \
# N: def __setitem__(self, slice, list[MyCInt | int | c_uint], /) -> None
for myx in mya:
reveal_type(myx) # N: Revealed type is "__main__.MyCInt | builtins.int"
[builtins fixtures/floatdict.pyi]
[typing fixtures/typing-medium.pyi]
[case testCtypesCharArrayAttrs]
import ctypes
ca = (ctypes.c_char * 4)(b'a', b'b', b'c', b'\x00')
reveal_type(ca.value) # N: Revealed type is "builtins.bytes"
reveal_type(ca.raw) # N: Revealed type is "builtins.bytes"
[builtins fixtures/floatdict.pyi]
[typing fixtures/typing-medium.pyi]
[case testCtypesCharPArrayDoesNotCrash]
import ctypes
# The following line used to crash with "Could not find builtin symbol 'NoneType'"
ca = (ctypes.c_char_p * 0)()
[builtins fixtures/floatdict.pyi]
[typing fixtures/typing-medium.pyi]
[case testCtypesWcharArrayAttrs]
import ctypes
wca = (ctypes.c_wchar * 4)('a', 'b', 'c', '\x00')
reveal_type(wca.value) # N: Revealed type is "builtins.str"
wca.raw # E: Array attribute "raw" is only available with element type "c_char", not "c_wchar"
[builtins fixtures/floatdict.pyi]
[typing fixtures/typing-medium.pyi]
[case testCtypesCharUnionArrayAttrs]
import ctypes
from typing import Union
cua: ctypes.Array[Union[ctypes.c_char, ctypes.c_wchar]]
reveal_type(cua.value) # N: Revealed type is "builtins.bytes | builtins.str"
cua.raw # E: Array attribute "raw" is only available with element type "c_char", not "c_char | c_wchar"
[builtins fixtures/floatdict.pyi]
[typing fixtures/typing-medium.pyi]
[case testCtypesAnyUnionArrayAttrs]
import ctypes
from typing import Any, Union
caa: ctypes.Array[Union[ctypes.c_char, Any]]
reveal_type(caa.value) # N: Revealed type is "builtins.bytes | Any"
reveal_type(caa.raw) # N: Revealed type is "builtins.bytes"
[builtins fixtures/floatdict.pyi]
[typing fixtures/typing-medium.pyi]
[case testCtypesOtherUnionArrayAttrs]
import ctypes
from typing import Union
cua: ctypes.Array[Union[ctypes.c_char, ctypes.c_int]]
cua.value # E: Array attribute "value" is only available with element type "c_char" or "c_wchar", not "c_char | c_int"
cua.raw # E: Array attribute "raw" is only available with element type "c_char", not "c_char | c_int"
[builtins fixtures/floatdict.pyi]
[typing fixtures/typing-medium.pyi]
[case testCtypesAnyArrayAttrs]
import ctypes
from typing import Any
aa: ctypes.Array[Any]
reveal_type(aa.value) # N: Revealed type is "Any"
reveal_type(aa.raw) # N: Revealed type is "builtins.bytes"
[builtins fixtures/floatdict.pyi]
[typing fixtures/typing-medium.pyi]
[case testCtypesOtherArrayAttrs]
import ctypes
oa = (ctypes.c_int * 4)(1, 2, 3, 4)
oa.value # E: Array attribute "value" is only available with element type "c_char" or "c_wchar", not "c_int"
oa.raw # E: Array attribute "raw" is only available with element type "c_char", not "c_int"
[builtins fixtures/floatdict.pyi]
[typing fixtures/typing-medium.pyi]
[case testCtypesArrayConstructorStarargs]
import ctypes
intarr4 = ctypes.c_int * 4
intarr6 = ctypes.c_int * 6
int_values = [1, 2, 3, 4]
c_int_values = [ctypes.c_int(1), ctypes.c_int(2), ctypes.c_int(3), ctypes.c_int(4)]
reveal_type(intarr4(*int_values)) # N: Revealed type is "_ctypes.Array[ctypes.c_int]"
reveal_type(intarr4(*c_int_values)) # N: Revealed type is "_ctypes.Array[ctypes.c_int]"
reveal_type(intarr6(1, ctypes.c_int(2), *int_values)) # N: Revealed type is "_ctypes.Array[ctypes.c_int]"
reveal_type(intarr6(1, ctypes.c_int(2), *c_int_values)) # N: Revealed type is "_ctypes.Array[ctypes.c_int]"
float_values = [1.0, 2.0, 3.0, 4.0]
intarr4(*float_values) # E: Array constructor argument 1 of type "list[float]" is not convertible to the array element type "Iterable[c_int]"
[builtins fixtures/floatdict.pyi]
[typing fixtures/typing-medium.pyi]
[case testCtypesArrayConstructorKwargs]
import ctypes
intarr4 = ctypes.c_int * 4
x = {"a": 1, "b": 2}
intarr4(**x)
[builtins fixtures/floatdict.pyi]
[typing fixtures/typing-medium.pyi]