简单 ctype 数组的类型注释

问题描述

在类型注释中使用 ctype 数组的正确方法是什么?

注意:我有 python2 代码,所以我使用的是注释样式类型注释

from ctypes import c_int32,Array

# create a new type (array of 100 int32)
MyArray = c_int32 * 100

def silly_func(
    my_array,# type: MyArray
    index,# type: int
):               # type: (...) -> c_int32
    return my_array[ index ]

mypy 给出了关于 MyArray 不是有效类型的错误

我尝试了 TypeVar、NewType 和 Array、Array[c_int32]、...的各种变体, 但找不到让mypy开心的东西。

有什么技巧???

解决方法

Array[c_int32] 使用 mypy 对我有用:

from ctypes import c_int32,Array
# create a new type (array of 100 int32)
MyArray = c_int32 * 100

def silly_func(
    my_array,# type: Array[c_int32]
    index,# type: int
):
    # type: (...) -> int
    return my_array[index]

silly_func(MyArray(),3)