Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python/mypy exhaustiveness checking with tuples

Tags:

python

match

mypy

I am trying to match on the value of a Union and have mypy perform exhaustiveness checking. Here is a minimal working example:

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

I would expect this to pass a mypy check, since both options are covered. But I get an error

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

This would indicate that there is a case missing for the value tuple[<nothing>, <nothing>]. I have been unable to find anything about limitations of matching tuples. Am I missing something or is this a mypy bug?

like image 502
Sebastiaan Avatar asked Sep 07 '26 19:09

Sebastiaan


1 Answers

Ok this seems to be a known issue. See

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

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

like image 85
Sebastiaan Avatar answered Sep 10 '26 09:09

Sebastiaan