*args 和 **kwargs 的类型注释
- 2025-02-08 08:46:00
- admin 原创
- 65
问题描述:
我正在尝试使用 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
?
解决方案 1:
对于可变位置参数(*args
)和可变关键字参数(**kw
),您只需要为一个这样的参数指定预期值。
来自类型提示PEP的任意参数列表和默认参数值部分:
任意参数列表也可以进行类型注释,以便定义:
def foo(*args: str, **kwds: int): ...
是可以接受的,这意味着,例如,以下所有内容都代表具有有效类型参数的函数调用:
foo('a', 'b', 'c') foo(x=1, y=2) foo('', z=0)
因此,您需要像这样指定您的方法:
def foo(*args: int):
但是,如果您的函数只能接受一个或两个整数值,则*args
根本不应该使用,而应该使用一个显式的位置参数和第二个关键字参数:
def foo(first: int, second: Optional[int] = None):
现在您的函数实际上仅限于一个或两个参数,并且如果指定,则两者都必须是整数。*args
始终表示 0 或更多,并且不能通过类型提示限制到更具体的范围。
解决方案 2:
2022 年更新
mypy 团队增加了对 的支持,自Mypy 0.981Unpack
或更高版本起可用。
注意!尽管此功能已完成,Unpack[...]
但仍被视为实验性的,因此您需要使用--enable-incomplete-features
来启用它。
您可以按如下方式使用此功能:
from typing import TypedDict
from typing_extensions import Unpack
class RequestParams(TypedDict):
url: str
allow_redirects: bool
def request(**kwargs: Unpack[RequestParams]) -> None:
...
如果使用request
TypedDict 中定义的参数调用该函数,则不会出现任何错误:
# OK
request(url="https://example.com", allow_redirects=True)
如果你忘记传递参数,mypy 会立即通知你
相关推荐
热门文章
项目管理软件有哪些?
热门标签
云禅道AD