Skip to content

Exhaustive checks with assert_never when using match on a 2-tuple fails #16650

Open
@MaxG87

Description

@MaxG87

Bug Report

I learned about assert_never, with which checks for exhaustiveness can be performed. I wanted to use it in a match statement where I want to cover all 4 possible scenarios of a tuple[int|None, int|None]. Unfortunately, when adding the catch-all case arm that contains typing.assert_never, I get a typing error indicating a non-exhaustive check above.

The error persists in a couple of variations. In particular, it persists when using 1-tuple. However, when matching on one value directly, using assert_never works as intended.

To Reproduce

https://gist.github.com/mypy-play/1df3d1b4c9e82226df17094a9f9c6af5
https://mypy-play.net/?mypy=latest&python=3.12&gist=1df3d1b4c9e82226df17094a9f9c6af5

Expected Behavior

All examples in the provided Gist should pass the type checks.

Actual Behavior

Mypy rejects the first three functions of the Gist. In particular, it's output reads:

Failed (exit code: 1) (2476 ms)

main.py:22: error: Argument 1 to "assert_never" has incompatible type "int | None"; expected "NoReturn" [arg-type]
main.py:43: error: Argument 1 to "assert_never" has incompatible type "int | None"; expected "NoReturn" [arg-type]
main.py:58: error: Argument 1 to "assert_never" has incompatible type "int | None"; expected "NoReturn" [arg-type]
Found 3 errors in 1 file (checked 1 source file)

Your Environment

Since I am using the Gist, I hope my particular environment doesn't matter much.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-match-statementPython 3.10's match statement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions