如何输入提示ctypes.POINTER(ctypes.c_int)

问题描述 投票:0回答:2

这是一个您可以运行的 MRE

mypy Playground

import ctypes
import numpy as np  # type: ignore # (no stubs for numpy)

def np_to_c(arr: np.ndarray) -> tuple[ctypes.POINTER(ctypes.c_int), ctypes.c_int]:
    return arr.ctypes.data_as(ctypes.POINTER(ctypes.c_int)), ctypes.c_int(len(arr))

这种类型的提示是错误的,根据

mypy

main.py:4: error: Invalid type comment or annotation  [valid-type]
main.py:4: note: Suggestion: use ctypes.POINTER[...] instead of ctypes.POINTER(...)
Found 1 error in 1 file (checked 1 source file)

但是当我使用

ctypes.POINTER[...]
而不是
ctypes.POINTER(...)
(参见下面的屏幕截图)时,我得到:

main.py:4: error: Function "ctypes.POINTER" is not valid as a type  [valid-type]
main.py:4: note: Perhaps you need "Callable[...]" or a callback protocol?
Found 1 error in 1 file (checked 1 source file)

enter image description here

python ctypes type-hinting
2个回答
0
投票

您可以在运行类型检查时专门使用不同的定义:

import ctypes
from typing import TYPE_CHECKING
import numpy as np  # type: ignore # (no stubs for numpy)

if TYPE_CHECKING:
    IntPointer = ctypes._Pointer[ctypes.c_int]
else:
    IntPointer = ctypes.POINTER(ctypes.c_int)

def np_to_c(arr: np.ndarray) -> tuple[IntPointer, ctypes.c_int]:
    return arr.ctypes.data_as(IntPointer), ctypes.c_int(len(arr))

-1
投票

ctypes.POINTER
是一个构造函数,它为作为其参数传递的 ctypes 数据类型 (
c_int
) 创建一个对象(指针)。因此,您的 linter 很可能会拒绝
ctypes.POINTER(ctypes.c_int)
作为类型提示,因为它实际上是一个函数调用。

ctypes.POINTER
返回的所有对象都源自
ctypes._Pointer
,所以我打赌会使用

def np_to_c(arr: np.ndarray) -> tuple[ctypes._Pointer, ctypes.c_int]:
    return arr.ctypes.data_as(ctypes.POINTER(ctypes.c_int)), ctypes.c_int(len(arr))

然而,这并不能真正表达返回的指针是专门用于

c_int
数组的。也有兴趣听到有关此事的更明智的意见!

© www.soinside.com 2019 - 2024. All rights reserved.