上下文
mypy
很好地检查类型处理是否正确。
# example.py
def test(string: str):
return str
if __name__ == '__main__':
test('19')
test(19)
这对第一种情况有效,但对第二种情况无效。
>> mypy example.py
example.py:6: error: Argument 1 to "test" has incompatible type "int"; expected "str"
但是,只需通过
>> python example.py
不会引发任何错误。
问题
在实际执行代码时,是否可以使用python中的可选静态类型来引发错误?
我希望用这个来检查变量类型
unittests
在那里,比各种各样的要容易得多
if not isinstance(...)
声明。