Skip to content

Structural Pattern Matching Exhaustiveness check when using OR #18108

Closed
@acciaioli

Description

@acciaioli

Bug Report

Mypy fails to identify missing value comparisons in my match when I use |

To Reproduce

Gist URL
Playground URL

from typing import Literal, assert_never

Color = Literal["blue", "green", "red"]


def print_color(color: Color) -> None:
  match color:
      case "blue":
          print("blue")
      case "green" | "kangaroo":
          print("green")
      case _:
          assert_never(color)

Expected Behavior

I expected mypy to error because I missed checking against red

colors.py:13: error: Argument 1 to "assert_never" has incompatible type "Literal['red']"; expected "Never"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

Actual Behavior

Mypy doesn't raise any errors

Success: no issues found in 1 source file

** Extra Context **

This only happens when I use |.
I don't use pyright but ran it to see if I was the one missing something but I believe they do flag it correctly

colors.py:13:24 - error: Argument of type "Literal['red']" cannot be assigned to parameter "arg" of type "Never" in function "assert_never"
    Type "Literal['red']" is not assignable to type "Never" (reportArgumentType)

Your Environment

  • Mypy version used: mypy 1.13.0 (compiled: yes)
  • Mypy command-line flags: --strict
  • Python version used: Python 3.11.6

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions