【发布时间】:2021-09-03 02:24:47
【问题描述】:
我有很多具有相同签名的函数,比如说(int, int) -> int。
有没有办法用Callable(或其他东西)键入这些函数,以避免为每个函数指定参数类型和返回类型?我想做这样的事情(但显然失败了):
from typing import Callable
f: Callable[[int, int], int]
def f(x, y): # with the previous line, this is equivalent to 'def f(x: int, y: int) -> int:'
...
运行 mypy 会导致:
file.py:4: error: Name "f" already defined on line 3
Found 1 error in 1 file (checked 1 source file)
【问题讨论】:
-
是的,确实如此。谢谢你。使用您的装饰器解决方案
mypy --strict仍然会在装饰功能上抱怨Function is missing a type annotation,但我想没有解决方案(除了禁用该行的类型检查)。 -
@MisterMiyagi:但是使用你的装饰器,mypy 无法猜测装饰函数中的参数类型,还是我错了?在您的示例中,如果我在
any_greet_person中执行x: int = name,mypy 不会抱怨。据我了解,只有装饰器的定义 (def copy_signature(template: C) -> Callable[[C], C]:) 有助于 mypy 猜测any_greet_person的类型是(name: str, age: int) -> str但这只是 outsideany_greet_person,不是是吗? -
确实,使用my answer 中显示的方法,函数的“内部”保持无类型。虽然我不知道解决方案(实际上我认为这是不可能改变的),但你是正确的,它与你想要的行为明显不同。
-
@MisterMiyagi:好的,感谢您的明确回答。最后一件事:做
setattr(target, "__annotations__", getattr(template, "__annotations__"))有什么意义,是为了动态类型检查吗?因为据我了解,mypy 并不关心这个,对吧?
标签: python type-hinting mypy python-typing