python/mypy 穷举检查元组

python/mypy exhaustiveness checking with tuples

提问人:Sebastiaan 提问时间:10/5/2023 更新时间:10/5/2023 访问量:55

问:

我正在尝试匹配 a 的值,并让 mypy 执行详尽性检查。下面是一个最小的工作示例:Union

t: tuple[int, float] | str
match t:
    case str():
        print("found str")
    case (int(), float()):
        print("found tuple")
    case _ as unreachable:
        assert_never(unreachable)

我希望它能通过 mypy 检查,因为这两个选项都包括在内。但是我收到一个错误

Argument 1 to "assert_never" has incompatible type "tuple[<nothing>, <nothing>]"; expected "NoReturn"  [arg-type]

这将表明缺少值 。 我一直找不到任何关于匹配元组限制的信息。是我遗漏了什么,还是这是一个mypy错误?casetuple[<nothing>, <nothing>]

python 匹配 mypy

评论

1赞 Robin Gugel 10/5/2023
似乎是一个mypy错误。Pyright 正确推断Neverunreachable

答:

1赞 Sebastiaan 10/5/2023 #1

好的,这似乎是一个已知问题。看

https://github.com/python/mypy/issues/14833

https://github.com/python/mypy/issues/12364