Python 数字输入的类型提示
Python Typehints for Numeric Input
我想为现有代码库中的数学函数添加类型提示。
大多数时候计算本身非常简单,可以用 pyhon numerics 和 numpy 数组来完成。
但是,我不确定如何键入以下函数:
def something(a, b):
return a + b
这是我已经尝试过的方法,我用 mypy 验证了类型提示。
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)
编辑:
错误很明显,所以我知道不允许添加 int
和 str
类型。因此,我不希望对所有错误进行澄清,而是希望有一个适用于我的用例的解决方案。
然而,我发现 something3
很有前途,但我不知道这里有什么问题。
Edit2:添加注释以便更容易地找到行号
Edit3:问题是不是关于序列的连接,而是关于将两个数字相加,将数字添加到数组以及将两个数组相加 .我只是使用'+'作为最简单的数学运算符。
使用 Python 3.8.5、numpy 1.20.2 和 mypy 0.812
我使用 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"
我想为现有代码库中的数学函数添加类型提示。 大多数时候计算本身非常简单,可以用 pyhon numerics 和 numpy 数组来完成。
但是,我不确定如何键入以下函数:
def something(a, b):
return a + b
这是我已经尝试过的方法,我用 mypy 验证了类型提示。
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)
编辑:
错误很明显,所以我知道不允许添加 int
和 str
类型。因此,我不希望对所有错误进行澄清,而是希望有一个适用于我的用例的解决方案。
然而,我发现 something3
很有前途,但我不知道这里有什么问题。
Edit2:添加注释以便更容易地找到行号
Edit3:问题是不是关于序列的连接,而是关于将两个数字相加,将数字添加到数组以及将两个数组相加 .我只是使用'+'作为最简单的数学运算符。
使用 Python 3.8.5、numpy 1.20.2 和 mypy 0.812
我使用 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"