Python *args 和 **kwargs 的类型注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37031928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Type annotations for *args and **kwargs
提问by Praxeolitic
I'm trying out Python's type annotations with abstract base classes to write some interfaces. Is there a way to annotate the possible types of *args
and **kwargs
?
我正在尝试使用抽象基类的 Python 类型注释来编写一些接口。有没有一种方法来注释可能的类型*args
和**kwargs
?
For example, how would one express that the sensible arguments to a function are either an int
or two int
s? type(args)
gives Tuple
so my guess was to annotate the type as Union[Tuple[int, int], Tuple[int]]
, but this doesn't work.
例如,如何表示函数的合理参数是一个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))
Error messages from mypy:
来自 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]]"
It makes sense that mypy doesn't like this for the function call because it expects there to be a tuple
in the call itself. The addition after unpacking also gives a typing error that I don't understand.
mypy 不喜欢这个函数调用是有道理的,因为它期望tuple
调用本身有一个。解包后的添加也给出了一个我不明白的打字错误。
How does one annotate the sensible types for *args
and **kwargs
?
如何为*args
and注释合理的类型**kwargs
?
回答by Martijn Pieters
For variable positional arguments (*args
) and variable keyword arguments (**kw
) you only need to specify the expected value for onesuch argument.
对于可变位置参数 ( *args
) 和可变关键字参数 ( **kw
),您只需指定一个此类参数的预期值。
From the Arbitrary argument lists and default argument valuessectionof the Type HintsPEP:
从任意参数列表和默认参数值部分的的类型提示PEP:
Arbitrary argument lists can as well be type annotated, so that the definition:
def foo(*args: str, **kwds: int): ...
is acceptable and it means that, e.g., all of the following represent function calls with valid types of arguments:
foo('a', 'b', 'c') foo(x=1, y=2) foo('', z=0)
任意参数列表也可以进行类型注释,以便定义:
def foo(*args: str, **kwds: int): ...
是可以接受的,这意味着,例如,以下所有内容都表示具有有效参数类型的函数调用:
foo('a', 'b', 'c') foo(x=1, y=2) foo('', z=0)
So you'd want to specify your method like this:
所以你想像这样指定你的方法:
def foo(*args: int):
However, if your function can only accept either one or two integer values, you should not use *args
at all, use one explicit positional argument and a second keyword argument:
但是,如果您的函数只能接受一个或两个整数值,则根本不应使用*args
,而是使用一个显式位置参数和第二个关键字参数:
def foo(first: int, second: Optional[int] = None):
Now your function is actually limited to one or two arguments, and both must be integers if specified. *args
alwaysmeans 0 or more, and can't be limited by type hints to a more specific range.
现在您的函数实际上仅限于一两个参数,并且如果指定,则两者都必须是整数。*args
总是意味着 0 或更多,并且不能被类型提示限制到更具体的范围。
回答by chadrik
The proper way to do this is using @overload
正确的方法是使用 @overload
from typing import overload
@overload
def foo(arg1: int, arg2: int) -> int:
...
@overload
def foo(arg: int) -> int:
...
def foo(*args):
try:
i, j = args
return i + j
except ValueError:
assert len(args) == 1
i = args[0]
return i
print(foo(1))
print(foo(1, 2))
Note that you do not add @overload
or type annotations to the actual implementation, which must come last.
请注意,您不要@overload
在实际实现中添加或键入注释,它必须放在最后。
You'll need a newish version of both typing
and mypy to get support for @overload outside of stub files.
你需要一个新版本的typing
和 mypy 才能在存根文件之外获得对@overload 的支持。
You can also use this to vary the returned result in a way that makes explicit which argument types correspond with which return type. e.g.:
您还可以使用它来改变返回的结果,以明确哪些参数类型对应于哪种返回类型。例如:
from typing import Tuple, overload
@overload
def foo(arg1: int, arg2: int) -> Tuple[int, int]:
...
@overload
def foo(arg: int) -> int:
...
def foo(*args):
try:
i, j = args
return j, i
except ValueError:
assert len(args) == 1
i = args[0]
return i
print(foo(1))
print(foo(1, 2))
回答by Michael0x2a
As a short addition to the previous answer, if you're trying to use mypy on Python 2 files and need to use comments to add types instead of annotations, you need to prefix the types for args
and kwargs
with *
and **
respectively:
作为短除了前面的答案,如果你想对Python的使用mypy 2个文件,需要使用的意见,而不是添加类型的注释,则需要前缀类型args
和kwargs
与*
和**
分别为:
def foo(param, *args, **kwargs):
# type: (bool, *str, **int) -> None
pass
This is treated by mypy as being the same as the below, Python 3.5 version of foo
:
这被 mypy 视为与下面的 Python 3.5 版本相同foo
:
def foo(param: bool, *args: str, **kwargs: int) -> None:
pass