小编kym*_*kym的帖子

如何输入hint函数以与numpy兼容

源代码example.py

from typing import Union, Any
import numpy as np

Number = Union[int, float, np.floating[Any]]


def add_one(num: Number) -> Number:
    return num + 1


inputs = [1, 2, 3]
outputs = [add_one(n) for n in inputs]

avg = np.mean(outputs)
Run Code Online (Sandbox Code Playgroud)

运行 mypy:

mypy example.py
src/example.py:14: error: Argument 1 to "mean" has incompatible type "List[Union[float, floating[Any]]]"; expected "Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]]"
Found 1 error in 1 file (checked 1 source …
Run Code Online (Sandbox Code Playgroud)

python numpy mypy python-typing

7
推荐指数
1
解决办法
2044
查看次数

标签 统计

mypy ×1

numpy ×1

python ×1

python-typing ×1