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?
Ok this seems to be a known issue. See
https://github.com/python/mypy/issues/14833
https://github.com/python/mypy/issues/12364
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With