Narrow individual items when matching a tuple to a sequence pattern by loic-simon · Pull Request #16905 · python/mypy

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!