match doesn't deduce type of tuple member from guard clause · Issue #12532 · python/mypy (original) (raw)

Bug Report

match can't determine type of an element of a tuple guarded with isinstance, if the variable holding the tuple has type Any.

To Reproduce

from typing import Any

e: Any = ('x',)
match e:
    case (a,) if isinstance(a, str):
        print('HERE', e)

Expected Behavior

This code should pass type-check. mypy should be able to determine the type of "a" because the isinstance guard implies that "a" must be a str.

Actual Behavior

$ mypy --python-version 3.10 x.py
x.py:5: error: Cannot determine type of "a"
Found 1 error in 1 file (checked 1 source file)

Variations

If e is given no declared type, like this:

then mypy reports no errors.

If e is declared to be a tuple whose members are unspecified, like this:

then mypy reports no errors.

Environment