【发布时间】:2022-01-01 19:42:40
【问题描述】:
我有一个如下所示的类:
from typing import Optional
import numpy as np
class TestClass():
def __init__(self, a: Optional[float] = None):
self.a = np.radians(a)
这会返回错误Argument 1 to "__call__" of "ufunc" has incompatible type "Optional[float]"; expected "Union[Union[int, float, complex, str, bytes, generic], Sequence[Union[int, float, complex, str, bytes, generic]], Sequence[Sequence[Any]], _SupportsArray]"
但是,即使它本质上做同样的事情,以下类也没有问题地通过:
from typing import Optional
import numpy as np
class TestClass():
def __init__(self, a: Optional[float] = None):
self.a = a
def test(self):
b = np.radians(self.a)
使用np.radians(None) 也没有任何影响。如何让 mypy 认识到这也会导致错误?
【问题讨论】:
标签: python type-hinting mypy