*args和**kwargs的类型批注

2024-09-27 09:35:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用抽象基类尝试Python的类型注释来编写一些接口。有没有办法注释*args**kwargs的可能类型?

例如,如何表示函数的敏感参数是一个int或两个ints?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的敏感类型?


Tags: topy类型footypeargserrorargument
3条回答

正确的方法是使用^{}

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))

请注意,您不会将@overload或类型注释添加到实际实现中,后者必须位于最后。

您需要一个新版本的typing和mypy来获得对@overloadoutside of stub files的支持。

您还可以使用此选项更改返回的结果,从而明确哪些参数类型与哪个返回类型对应。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))

作为前一个答案的简短补充,如果您试图在Python 2文件上使用mypy,并且需要使用注释来添加类型而不是注释,那么您需要在类型的前面分别加上argskwargs的前缀,分别是***

def foo(param, *args, **kwargs):
    # type: (bool, *str, **int) -> None
    pass

mypy将其视为与以下Python 3.5版本的foo相同:

def foo(param: bool, *args: str, **kwargs: int) -> None:
    pass

对于变量位置参数(*args)和变量关键字参数(**kw),您只需要为一个这样的参数指定期望值。

类型提示PEP的Arbitrary argument lists and default argument values section

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: int):

但是,如果函数只能接受一个或两个整数值,则根本不应使用*args,请使用一个显式位置参数和第二个关键字参数:

def foo(first: int, second: Optional[int] = None):

现在您的函数实际上被限制为一个或两个参数,如果指定的话,这两个参数都必须是整数。*args始终表示0或更多,并且不能被类型提示限制在更特定的范围内。

相关问题 更多 >

    热门问题