Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ctypes nested array constructor doesn't work with sequences #6212

Open
alanhdu opened this issue Jan 17, 2019 · 1 comment
Open

ctypes nested array constructor doesn't work with sequences #6212

alanhdu opened this issue Jan 17, 2019 · 1 comment
Labels
bug mypy got something wrong priority-1-normal topic-plugins The plugin API and ideas for new plugins

Comments

@alanhdu
Copy link
Contributor

alanhdu commented Jan 17, 2019

Using Python 3.7 and mypy 0.660 on the following code:

import ctypes

Quaternion = ctypes.c_int * 4
Pair = Quaternion * 2

quat = Quaternion(1, 2, 3, 4)
p = Pair((1, 2, 3, 4), (2, 3, 4, 5))

returns

$ mypy ctypes_nested.py 
ctypes_nested.py:7: error: Array constructor argument 1 of type "Tuple[builtins.int, builtins.int, builtins.int, builtins.int]" is not convertible to the array element type "ctypes.Array[ctypes.c_int]"
ctypes_nested.py:7: error: Array constructor argument 2 of type "Tuple[builtins.int, builtins.int, builtins.int, builtins.int]" is not convertible to the array element type "ctypes.Array[ctypes.c_int]"

even though at runtime this constructor works just fine. Of course, changing the constructor to Pair(Quaternion(1, 2, 3, 4), Quaternion(2, 3, 4, 5)) works just fine.

@ilevkivskyi ilevkivskyi added bug mypy got something wrong priority-1-normal topic-plugins The plugin API and ideas for new plugins labels Jan 17, 2019
@jameslamb
Copy link

jameslamb commented Nov 14, 2023

Using Python 3.11.5 and mypy 1.6.1, I experienced what I think is a related issue today.

I'm fairly confident the following is valid ctypes code. It follows the pattern for array construction recommended by https://docs.python.org/3/library/ctypes.html#arrays.

The recommended way to create array types is by multiplying a data type with a positive integer

And follows the docs for ctypes.c_char_p at https://docs.python.org/3/library/ctypes.html#ctypes.c_char_p ctypes.c_char_p, which say

The constructor accepts an integer address

import ctypes

buffers = [
    ctypes.create_string_buffer(255),
    ctypes.create_string_buffer(255)
]

CharArray = ctypes.c_char_p * 2

array_of_buffer_pointers = CharArray(
    ctypes.addressof(buffers[0]),
    ctypes.addressof(buffers[1])
)

That code runs without error and works the way I'd expect, but mypy is unhappy about it.

mypy ./test-ctypes.py

# test-ctypes.py:10: error: Array constructor argument 1 of type "int" is not convertible to the array element type "c_char_p"  [misc]
# test-ctypes.py:10: error: Array constructor argument 2 of type "int" is not convertible to the array element type "c_char_p"  [misc]
# Found 2 errors in 1 file (checked 1 source file)

Searching for that error message in this project, I found this test:

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]

It and similar tests in that file appear to all check how the case where literal values in Python objects are provided to fill ctypes arrays, but not the cases where integers representing addresses are used.

I apologize if I've made some mistake in my use of ctypes here or if this is not related the original post above. I hope this report will help narrow down the issue if there is one.

Thanks for your time and consideration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-1-normal topic-plugins The plugin API and ideas for new plugins
Projects
None yet
Development

No branches or pull requests

3 participants