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

Incorrect dtype-ctypes static typing #19467

Closed
mixilchenko opened this issue Jul 13, 2021 · 1 comment · Fixed by #19468 or #19503
Closed

Incorrect dtype-ctypes static typing #19467

mixilchenko opened this issue Jul 13, 2021 · 1 comment · Fixed by #19468 or #19503

Comments

@mixilchenko
Copy link

I have some c++ - python compatible features and use ctypes lib for creating dtypes dynamically.
np.dtype typing is incorrect when ctypes type is argument.

Reproducing code example:

# myproblem.py
import ctypes
from typing import TYPE_CHECKING

import numpy as np


dtype_int = np.dtype(ctypes.c_int)
dtype_size_t = np.dtype(ctypes.c_size_t)
print(repr(dtype_int))  # prints `dtype('int32')>` as expected on my machine
print(repr(dtype_size_t))  # prints `dtype('uint64')` as expected on my machine
if TYPE_CHECKING:
    reveal_type(dtype_int)  # something like `numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]` is expected
    reveal_type(dtype_size_t)  # something like `numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]` is expected

Mypy error message:

$ mypy test.py
myproblem.py:8: error: Value of type variable "_DTypeScalar_co" of "dtype" cannot be "c_int"
myproblem.py:9: error: Value of type variable "_DTypeScalar_co" of "dtype" cannot be "c_size_t"
myproblem.py:13: note: Revealed type is "numpy.dtype[ctypes.c_int*]"
myproblem.py:14: note: Revealed type is "numpy.dtype[ctypes.c_size_t*]"
Found 2 errors in 1 file (checked 1 source file)

NumPy/Python version information:

>>> import sys; from importlib.metadata import version; print(sys.version, version('mypy'), version('numpy'), sep='\n')
3.9.1 (default, Jul  7 2021, 22:39:47)
[Clang 12.0.5 (clang-1205.0.22.9)]
0.910
1.21.0
@BvB93
Copy link
Member

BvB93 commented Jul 13, 2021

Ah, I though we had a catchall overload for miscellaneous typeobjects, but it seems to have been removed at some point.

In any case, #19468 should add the necasary overloads for the ctypes types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants