あとで調べる:mypyで分割代入でのエラーはtyping.Tupleなどの型のときしかみない?

以下のコードが通ってしまう。

import typing as t


def f() -> t.Mapping:
    return {}


def use() -> None:
    z = f()
    y, z = f()
    x, y, z = f()


if __name__ == "__main__":
    import subprocess
    subprocess.check_call(["mypy", "--strict", __file__])
    print("ok")

たしかにこれだけ見ると、iterableにしてそこからN個取るという処理なので、型チェックだけに終止しているとuse()の部分でエラーにならないのかも。

typing.Tupleは型に長さ自体の情報があるのでしっかり失敗してくれる模様。

def g() -> t.Tuple[t.Mapping]:
    return ({},)


def use() -> None:
    z = g()
    y, z = g()  # ng
    x, y, z = g()  # ng
02.py:10: error: Need more than 1 value to unpack (2 expected)
02.py:11: error: Need more than 1 value to unpack (3 expected)

ただ

ただ、strでも型チェックは通過してしまうっぽい。

def f() -> str:
    return ""


def use() -> None:
    z = f()
    y, z = f()
    x, y, z = f()

ちなみに、intの場合はiterableではないので型チェックを通らない(error: 'builtins.int' object is not iterable)。