File: check-ctypes.test

package info (click to toggle)
mypy 1.19.1-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 22,464 kB
  • sloc: python: 114,757; ansic: 13,343; cpp: 11,380; makefile: 254; sh: 31
file content (183 lines) | stat: -rw-r--r-- 7,673 bytes parent folder | download | duplicates (4)
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, Union[c_int, int], /) -> None \
                 # N:     def __setitem__(self, slice, list[Union[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, Union[MyCInt, int], /) -> None \
                           # N:     def __setitem__(self, slice, list[Union[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, Union[MyCInt, int], /) -> None \
                   # N:     def __setitem__(self, slice, list[Union[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 "Union[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[Union[__main__.MyCInt, ctypes.c_uint]]"
reveal_type(mya[0])  # N: Revealed type is "Union[__main__.MyCInt, builtins.int]"
reveal_type(mya[1:3])  # N: Revealed type is "builtins.list[Union[__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, Union[MyCInt, int, c_uint], /) -> None \
                   # N:     def __setitem__(self, slice, list[Union[MyCInt, int, c_uint]], /) -> None
for myx in mya:
    reveal_type(myx)  # N: Revealed type is "Union[__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 "Union[builtins.bytes, builtins.str]"
cua.raw  # E: Array attribute "raw" is only available with element type "c_char", not "Union[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 "Union[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 "Union[c_char, c_int]"
cua.raw  # E: Array attribute "raw" is only available with element type "c_char", not "Union[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]"
[typing fixtures/typing-medium.pyi]

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]

[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]