【发布时间】:2018-06-17 09:01:34
【问题描述】:
使用mypy 对以下代码进行类型检查:
def foo(a: str, b: float, c: int):
print(a, b, c + 1)
foo('ok', 2.2, 'bad')
也显示了无效调用foo:
error: Argument 3 to "foo" has incompatible type "str"; expected "int"
现在假设我们有一个如下所示的包装函数:
from typing import Callable, Any
def say_hi_and_call(func: Callable[..., Any], *args):
print('Hi.')
func(*args)
并使用它进行无效调用
say_hi_and_call(foo, 'ok', 2.2, 'bad')
mypy 不会报告任何错误,而是我们只会在运行时了解此错误:
TypeError: must be str, not int
我想早点发现这个错误。是否有可能以mypy 能够报告问题的方式细化类型注释?
【问题讨论】:
-
@Kasramvd OP 希望 mypy 将
say_hi_and_call(foo, 'ok', 2.2, 'bad')报告为错误。
标签: python types python-3.6 typechecking mypy