使用 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 能够报告问题的方式改进类型注释?
请您参考如下方法:
好吧,我想出的唯一解决方案是使函数的元数显式化,即
from typing import Any, Callable, TypeVar
A = TypeVar('A')
B = TypeVar('B')
C = TypeVar('C')
def say_hi_and_call_ternary(func: Callable[[A, B, C], Any], a: A, b: B, c: C):
print('Hi.')
func(a, b, c)
def foo(a: str, b: float, c: int):
print(a, b, c + 1)
say_hi_and_call_ternary(foo, 'ok', 2.2, 'bad')
当然也需要类似的 say_hi_and_call_unary 和 say_hi_and_call_binary 等。
但是由于我认为我的应用程序不会在 PROD 中爆炸而不是节省一些 LOC,所以当 mypy 能够报告错误时我很高兴,现在确实是这样:
error: Argument 1 to "say_hi_and_call_ternary" has incompatible type "Callable[[str, float, int], Any]"; expected "Callable[[str, float, str], Any]"
