-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Open
Labels
bugmypy got something wrongmypy got something wrongtopic-match-statementPython 3.10's match statementPython 3.10's match statementtopic-type-narrowingConditional type narrowing / binderConditional type narrowing / binder
Description
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)
lmeninato
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrongtopic-match-statementPython 3.10's match statementPython 3.10's match statementtopic-type-narrowingConditional type narrowing / binderConditional type narrowing / binder