我正在用抽象基类尝试Python的类型注释来编写一些接口。有没有办法注释*args
和**kwargs
的可能类型?
例如,如何表示函数的敏感参数是一个int
或两个int
s?type(args)
给出Tuple
所以我的猜测是将类型注释为Union[Tuple[int, int], Tuple[int]]
,但这不起作用。
from typing import Union, Tuple
def foo(*args: Union[Tuple[int, int], Tuple[int]]):
try:
i, j = args
return i + j
except ValueError:
assert len(args) == 1
i = args[0]
return i
# ok
print(foo((1,)))
print(foo((1, 2)))
# mypy does not like this
print(foo(1))
print(foo(1, 2))
来自mypy的错误消息:
t.py: note: In function "foo":
t.py:6: error: Unsupported operand types for + ("tuple" and "Union[Tuple[int, int], Tuple[int]]")
t.py: note: At top level:
t.py:12: error: Argument 1 to "foo" has incompatible type "int"; expected "Union[Tuple[int, int], Tuple[int]]"
t.py:14: error: Argument 1 to "foo" has incompatible type "int"; expected "Union[Tuple[int, int], Tuple[int]]"
t.py:15: error: Argument 1 to "foo" has incompatible type "int"; expected "Union[Tuple[int, int], Tuple[int]]"
t.py:15: error: Argument 2 to "foo" has incompatible type "int"; expected "Union[Tuple[int, int], Tuple[int]]"
mypy不喜欢这个函数调用是有道理的,因为它希望调用本身有一个tuple
。解包后的添加也会产生一个我不理解的输入错误。
如何注释*args
和**kwargs
的敏感类型?
正确的方法是使用^{}
请注意,您不会将
@overload
或类型注释添加到实际实现中,后者必须位于最后。您需要一个新版本的
typing
和mypy来获得对@overloadoutside of stub files的支持。您还可以使用此选项更改返回的结果,从而明确哪些参数类型与哪个返回类型对应。e、 g.:
作为前一个答案的简短补充,如果您试图在Python 2文件上使用mypy,并且需要使用注释来添加类型而不是注释,那么您需要在类型的前面分别加上
args
和kwargs
的前缀,分别是*
和**
:mypy将其视为与以下Python 3.5版本的
foo
相同:对于变量位置参数(
*args
)和变量关键字参数(**kw
),您只需要为一个这样的参数指定期望值。从类型提示PEP的Arbitrary argument lists and default argument values section:
所以您需要指定如下方法:
但是,如果函数只能接受一个或两个整数值,则根本不应使用
*args
,请使用一个显式位置参数和第二个关键字参数:现在您的函数实际上被限制为一个或两个参数,如果指定的话,这两个参数都必须是整数。
*args
始终表示0或更多,并且不能被类型提示限制在更特定的范围内。相关问题 更多 >
编程相关推荐