用于数字输入的Python类型提示

2024-05-18 22:28:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我想为现有代码库中的数学函数添加类型提示。 大多数情况下,计算本身非常简单,可以使用pyhon numerics和numpy数组来完成

但是,我不确定如何键入以下函数:

def something(a, b):
    return a + b

这是我已经尝试过的,我用mypy验证了typehints

from typing import TypeVar, Union
from numbers import Number
import numpy.typing as npt
import numpy as np

def something1(a:npt.ArrayLike, b: npt.ArrayLike) -> npt.ArrayLike:
    return a + b #7
x1 = something1(3,4)

T2 = TypeVar('T2')
def something2(a: T2, b: T2) -> T2:
    return a + b #12
x2 = something2(3,4)

T3 = TypeVar('T3', bound=Union[Number, np.ndarray])
def something3(a: T3, b: T3) -> T3:
    return a + b #17
x3 = something3(3,4)

T4 = TypeVar('T4', bound=np.ndarray)
def something4(a: T4, b: T4) -> T4:
    return a + b # This works, but not with an python number
x4 = something4(3,4) #23

T5 = TypeVar('T5', bound=npt.ArrayLike)
def something5(a: T5, b: T5) -> T5:
    return a + b #27
x5 = something5(3,4)

T6 = TypeVar('T6', bound=Number)
def something6(a: T6, b: T6) -> T6:
    return a + b #32
x6 = something6(3,4) #33

mypy对每个函数都会抛出几个错误,因此我不确定如何继续。 这是mypy输出

runsomething.py:7: error: Unsupported operand types for + ("int" and "str")
runsomething.py:7: error: Unsupported operand types for + ("int" and "bytes")
runsomething.py:7: error: Unsupported operand types for + ("int" and "generic")
runsomething.py:7: error: Unsupported operand types for + ("int" and "Sequence[Union[int, float, complex, str, bytes, generic]]")
runsomething.py:7: error: Unsupported operand types for + ("int" and "Sequence[Sequence[Any]]")
runsomething.py:7: error: Unsupported operand types for + ("int" and "_SupportsArray")
runsomething.py:7: error: Unsupported operand types for + ("float" and "str")
runsomething.py:7: error: Unsupported operand types for + ("float" and "bytes")
runsomething.py:7: error: Unsupported operand types for + ("float" and "generic")
runsomething.py:7: error: Unsupported operand types for + ("float" and "Sequence[Union[int, float, complex, str, bytes, generic]]")
runsomething.py:7: error: Unsupported operand types for + ("float" and "Sequence[Sequence[Any]]")
runsomething.py:7: error: Unsupported operand types for + ("float" and "_SupportsArray")
runsomething.py:7: error: Unsupported operand types for + ("complex" and "str")
runsomething.py:7: error: Unsupported operand types for + ("complex" and "bytes")
runsomething.py:7: error: Unsupported operand types for + ("complex" and "generic")
runsomething.py:7: error: Unsupported operand types for + ("complex" and "Sequence[Union[int, float, complex, str, bytes, generic]]")
runsomething.py:7: error: Unsupported operand types for + ("complex" and "Sequence[Sequence[Any]]")
runsomething.py:7: error: Unsupported operand types for + ("complex" and "_SupportsArray")
runsomething.py:7: error: Unsupported operand types for + ("str" and "int")
runsomething.py:7: error: Unsupported operand types for + ("str" and "float")
runsomething.py:7: error: Unsupported operand types for + ("str" and "complex")
runsomething.py:7: error: Unsupported operand types for + ("str" and "bytes")
runsomething.py:7: error: Unsupported operand types for + ("str" and "generic")
runsomething.py:7: error: Unsupported operand types for + ("str" and "Sequence[Union[int, float, complex, str, bytes, generic]]")
runsomething.py:7: error: Unsupported operand types for + ("str" and "Sequence[Sequence[Any]]")
runsomething.py:7: error: Unsupported operand types for + ("str" and "_SupportsArray")
runsomething.py:7: error: Unsupported operand types for + ("bytes" and "int")
runsomething.py:7: error: Unsupported operand types for + ("bytes" and "float")
runsomething.py:7: error: Unsupported operand types for + ("bytes" and "complex")
runsomething.py:7: error: Unsupported operand types for + ("bytes" and "str")
runsomething.py:7: error: Unsupported operand types for + ("bytes" and "generic")
runsomething.py:7: error: Unsupported operand types for + ("bytes" and "Sequence[Union[int, float, complex, str, bytes, generic]]")
runsomething.py:7: error: Unsupported operand types for + ("bytes" and "Sequence[Sequence[Any]]")
runsomething.py:7: error: Unsupported operand types for + ("bytes" and "_SupportsArray")
runsomething.py:7: error: Unsupported operand types for + ("generic" and "int")
runsomething.py:7: error: Unsupported operand types for + ("generic" and "float")
runsomething.py:7: error: Unsupported operand types for + ("generic" and "complex")
runsomething.py:7: error: Unsupported left operand type for + ("generic")
runsomething.py:7: error: Unsupported operand types for + ("Sequence[Union[int, float, complex, str, bytes, generic]]" and "int")
runsomething.py:7: error: Unsupported operand types for + ("Sequence[Union[int, float, complex, str, bytes, generic]]" and "float")
runsomething.py:7: error: Unsupported operand types for + ("Sequence[Union[int, float, complex, str, bytes, generic]]" and "complex")
runsomething.py:7: error: Unsupported left operand type for + ("Sequence[Union[int, float, complex, str, bytes, generic]]")
runsomething.py:7: error: Unsupported operand types for + ("Sequence[Sequence[Any]]" and "int")
runsomething.py:7: error: Unsupported operand types for + ("Sequence[Sequence[Any]]" and "float")
runsomething.py:7: error: Unsupported operand types for + ("Sequence[Sequence[Any]]" and "complex")
runsomething.py:7: error: Unsupported left operand type for + ("Sequence[Sequence[Any]]")
runsomething.py:7: error: Unsupported operand types for + ("_SupportsArray" and "int")
runsomething.py:7: error: Unsupported operand types for + ("_SupportsArray" and "float")
runsomething.py:7: error: Unsupported operand types for + ("_SupportsArray" and "complex")
runsomething.py:7: error: Unsupported left operand type for + ("_SupportsArray")
runsomething.py:7: note: Both left and right operands are unions
runsomething.py:12: error: Unsupported left operand type for + ("T2")
runsomething.py:17: error: Unsupported left operand type for + (some union)
runsomething.py:17: error: Unsupported operand types for + ("ndarray" and "T3")
runsomething.py:18: error: Value of type variable "T3" of "something3" cannot be "int"
runsomething.py:23: error: Value of type variable "T4" of "something4" cannot be "int"
runsomething.py:27: error: Unsupported left operand type for + (some union)
runsomething.py:27: error: Incompatible return value type (got "Union[complex, str, bytes, Any]", expected "T5")
runsomething.py:27: error: Unsupported operand types for + ("int" and "T5")
runsomething.py:27: error: Unsupported operand types for + ("float" and "T5")
runsomething.py:27: error: Unsupported operand types for + ("complex" and "T5")
runsomething.py:27: error: Unsupported operand types for + ("str" and "T5")
runsomething.py:27: error: Unsupported operand types for + ("bytes" and "T5")
runsomething.py:32: error: Unsupported left operand type for + ("T6")
runsomething.py:33: error: Value of type variable "T6" of "something6" cannot be "int"
Found 64 errors in 1 file (checked 1 source file)

编辑: 错误很明显,所以我知道不允许添加intstr类型。因此,我不希望对所有的错误进行澄清,而是希望有一个适合我的用例的解决方案。 然而,我发现something3很有希望,但我不知道这里的问题是什么

Edit2:添加注释以便于识别行号

编辑3:问题不是关于序列的串联,而是关于添加两个数字、向数组添加数字和添加两个数组。我只是用“+”作为最简单的数学运算符

使用Python 3.8.5、numpy 1.20.2和mypy 0.812


Tags: andpyforbyteserrorfloatgenericint
1条回答
网友
1楼 · 发布于 2024-05-18 22:28:23

我用Protocol实现了这个

from typing import TypeVar, overload, Protocol

V = TypeVar("V", contravariant=True)
R = TypeVar("R", covariant=True)

A = TypeVar("A")
B = TypeVar("B")

class CanAdd(Protocol[V, R]):
    def __add__(self, other: V) -> R: ...

@overload
def add(a: CanAdd[A, B], b: A) -> B: ...
@overload
def add(a: A, b: CanAdd[A, B]) -> B: ...

def add(a, b):
    return a + b

add(1, 1)
add("abc", "123")
add(1, "abc") # error: Cannot infer type argument 1 of "add"

相关问题 更多 >

    热门问题