Debian Codegraph @

test-data/unit/check-ctypes.test

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