mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Match exhaustion fails incorrectly when matching same option twice.

Open superosku opened this issue 1 year ago • 0 comments

Mypy shows an incorrect error when the same option is twice in an match statement.

To Reproduce

from typing import assert_never
from enum import Enum, auto


class Medal(Enum):
    gold: auto
    silver: auto
    bronze: auto


def fail_1(medal: Medal) -> None:
    match medal:
        case Medal.gold:
            pass
        case Medal.silver | Medal.silver:
            pass
        case Medal.bronze:
            pass
        case _:
            assert_never(medal)  # is "Literal[Medal.silver]" should be NoReturn


def fail_2(medal: Medal) -> None:
    match medal:
        case Medal.gold:
            pass
        case Medal.silver | Medal.silver:
            pass
        case _:
            assert_never(medal)  # is "Literal[Medal.silver]" should be "Literal[Medal.bronze]"

Expected Behavior

See the comments in the example python code.

It should either fail on case Medal.silver | Medal.silver: or fail_1 should assert never properly and fail_2 should show Medal.bronze instead of Medal.silver

Actual Behavior

See the python example.

Your Environment

Mypy version used:

 $ mypy --version
mypy 1.10.0 (compiled: yes)

Python version used:

 $ python --version
Python 3.12.2

Mypy command-line flags: none

Mypy configuration options from mypy.ini (and other config files): none

superosku avatar Jun 13 '24 00:06 superosku