| [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 "builtins.str" is not convertible to the array element type "ctypes.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] |
| |
| [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 "ctypes.c_int" is not convertible to the array element type "__main__.MyCInt" \ |
| # E: Array constructor argument 4 of type "builtins.str" is not convertible to the array element type "__main__.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] |
| |
| [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] |
| |
| [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] |
| |
| [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] |
| |
| [case testCtypesCharArrayAttrsPy2] |
| # flags: --py2 |
| import ctypes |
| |
| ca = (ctypes.c_char * 4)('a', 'b', 'c', '\x00') |
| reveal_type(ca.value) # N: Revealed type is 'builtins.str' |
| reveal_type(ca.raw) # N: Revealed type is 'builtins.str' |
| [builtins_py2 fixtures/floatdict_python2.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: ctypes.Array attribute "raw" is only available with element type c_char, not "ctypes.c_wchar" |
| [builtins fixtures/floatdict.pyi] |
| |
| [case testCtypesWcharArrayAttrsPy2] |
| # flags: --py2 |
| import ctypes |
| |
| wca = (ctypes.c_wchar * 4)(u'a', u'b', u'c', u'\x00') |
| reveal_type(wca.value) # N: Revealed type is 'builtins.unicode' |
| wca.raw # E: ctypes.Array attribute "raw" is only available with element type c_char, not "ctypes.c_wchar" |
| [builtins_py2 fixtures/floatdict_python2.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: ctypes.Array attribute "raw" is only available with element type c_char, not "Union[ctypes.c_char, ctypes.c_wchar]" |
| [builtins fixtures/floatdict.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] |
| |
| [case testCtypesOtherUnionArrayAttrs] |
| import ctypes |
| from typing import Union |
| |
| cua: ctypes.Array[Union[ctypes.c_char, ctypes.c_int]] |
| cua.value # E: ctypes.Array attribute "value" is only available with element type c_char or c_wchar, not "Union[ctypes.c_char, ctypes.c_int]" |
| cua.raw # E: ctypes.Array attribute "raw" is only available with element type c_char, not "Union[ctypes.c_char, ctypes.c_int]" |
| [builtins fixtures/floatdict.pyi] |
| |
| [case testCtypesAnyArrayAttrs] |
| import ctypes |
| |
| 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] |
| |
| [case testCtypesOtherArrayAttrs] |
| import ctypes |
| |
| oa = (ctypes.c_int * 4)(1, 2, 3, 4) |
| oa.value # E: ctypes.Array attribute "value" is only available with element type c_char or c_wchar, not "ctypes.c_int" |
| oa.raw # E: ctypes.Array attribute "raw" is only available with element type c_char, not "ctypes.c_int" |
| [builtins fixtures/floatdict.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 "builtins.list[builtins.float*]" is not convertible to the array element type "Iterable[ctypes.c_int]" |
| [builtins fixtures/floatdict.pyi] |
| |
| [case testCtypesArrayConstructorKwargs] |
| import ctypes |
| intarr4 = ctypes.c_int * 4 |
| |
| x = {"a": 1, "b": 2} |
| intarr4(**x) # E: Too many arguments for "Array" |
| |
| [builtins fixtures/floatdict.pyi] |