下面我定义了类型变量、泛型类型别名和一个点积函数。mypy不会引发错误。为什么不?
我希望它会引发错误,v3因为它是一个字符串向量,并且我已经指定它T必须是int, float, 或complex。
from typing import Any, Iterable, Tuple, TypeVar
T = TypeVar('T', int, float, complex)
Vector = Iterable[T]
def dot_product(a: Vector[T], b: Vector[T]) -> T:
return sum(x * y for x, y in zip(a, b))
v1: Vector[int] = [] # same as Iterable[int], OK
v2: Vector[float] = [] # same as Iterable[float], OK
v3: Vector[str] = [] # no error - why not?
繁华开满天机
相关分类