Skip to content

No narrowing of sequence types after a sequence pattern case in a match statement #13955

@dmoisset

Description

@dmoisset

Bug Report

When a pattern matching statement contains a pattern that would match any sequence (for example case (*elements)), subsequence cases should be narrow to the non-sequence possibilities

To Reproduce

def f(x: int | list[int]) -> int:
    match x:
        case [*foo]:
            return len(foo)
        case _:
            reveal_type(x)
            return x

Expected Behavior

The code above should type-check correctly. The revealed type for x should be int.

Actual Behavior

main.py:6: note: Revealed type is "Union[builtins.int, builtins.list[builtins.int]]"
main.py:7: error: Incompatible return value type (got "Union[int, List[int]]", expected "int")
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.981
  • Mypy command-line flags: none
  • Python version used: 3.10

As an additional note, the behaviour I expect can be observed using pyright (the one integrated in vscode 1.72 with PyLance 2022.10.40)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-match-statementPython 3.10's match statementtopic-type-narrowingConditional type narrowing / binder

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions