Я пробую аннотации типов Python с абстрактными базовыми классами для написания некоторых интерфейсов. Есть ли способ аннотировать возможные типы *args
и **kwargs
?
Например, как выразить, что разумные аргументы функции - это либо int
, либо два int
? 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
?