Skip to content

Tuple matching doesn’t recognize exhaustive OR patterns #5622

Open

Description

@zshuzh

Describe the Bug

def with_or_pattern(value: Literal[1, 2]) -> None:
    match (value,):
        case (1 | 2,):
            pass
        case remaining:
            assert_never(remaining)  # Rejected


def with_separate_cases(value: Literal[1, 2]) -> None:
    match (value,):
        case (1,):
            pass
        case (2,):
            pass
        case remaining:
            assert_never(remaining)  # Accepted

Specifically, the error for with_or_pattern is:

ERROR Argument `tuple[Literal[1, 2]]` is not assignable to parameter `arg` with type `Never` in function `typing.assert_never` [bad-argument-type]

I'd expect both forms to be accepted: (1 | 2,) should cover the same values as the two separate cases (1,) and (2,).

Using an OR between whole tuple patterns also works:

case (1,) | (2,):
   pass

It seems specifically placing the OR inside a tuple element prevents Pyrefly from recognizing that all values have been covered.

Sandbox Link

Sandbox

(Only applicable for extension issues) IDE Information

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions