mypyの可変長な引数と戻り値の型の書き方

可変長引数

import typing as t
import mypy_extensions as mx

def f0() -> t.List[int]:
    return []


def f1(x0: int) -> t.List[int]:
    return [x0]


def f2(x0: int, x1: int) -> t.List[int]:
    return [x0, x1]


def fN(*xs: int) -> t.List[int]:
    return list(xs)

Callableの方ではVarArgを使う。

import typing as t
import mypy_extensions as mx

def g(f: t.Callable[[mx.VarArg(int)], t.List[int]], *xs: int) -> t.List[int]:
    return f(*xs)

可変長な戻り値

tupleが以下なので

import typing as t

x0 = ()  # type: t.Tuple
x1 = (1, )  # type: t.Tuple[int]
x2 = (1, 2)  # type: t.Tuple[int, int]

y0 = ()  # type: t.Tuple[int, ...]
y1 = (1, )  # type: t.Tuple[int, ...]
y2 = (1, 2)  # type: t.Tuple[int, ...]

関数の戻り値のときも同様に。

def h0(x: int) -> t.Tuple:
    return ()


def h1(x: int) -> t.Tuple[int, ]:
    return (x, )


def h2(x: int) -> t.Tuple[int, int]:
    return (x, x)


def hN(x: int) -> t.Tuple[int, ...]:
    return (x, x, x, x)

ちなみにtupleとlistを区別したくない場合にはSequenceを使う

def kN(x: int) -> t.Sequence[int]:
    return (x, x, x, x)


def kM(x: int) -> t.Sequence[int]:
    return [x, x, x, x]

参考