mypy
mypy copied to clipboard
Narrow individual items when matching a tuple to a sequence pattern
Fixes #12364
When matching a tuple to a sequence pattern, this change narrows the type of tuple items inside the matched case:
def test(a: bool, b: bool) -> None:
match a, b:
case True, True:
reveal_type(a) # before: "builtins.bool", after: "Literal[True]"
This also works with nested tuples, recursively:
def test(a: bool, b: bool, c: bool) -> None:
match a, (b, c):
case _, [True, False]:
reveal_type(c) # before: "builtins.bool", after: "Literal[False]"
This only partially fixes issue #12364; see my comment there for more context.
This is my first contribution to mypy, so I may miss some context or conventions; I'm eager for any feedback!
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅