Я намагаюсь примітки типу 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?
Optional? Щось змінилося в Python чи ви передумали? Це все-таки не є строго необхідним черезNoneдефолт?