Skip to content

match doesn't deduce type of tuple member from guard clause #12532

@bkovitz

Description

@bkovitz

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:

e = ('x',)

then mypy reports no errors.

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

e: Tuple = ('x',)

then mypy reports no errors.

Environment

  • Mypy version used: 0.942
  • Mypy command-line flags: --python-version 3.10 x.py
  • Mypy configuration options from mypy.ini (and other config files): (none)
  • Python version used: 3.10.2
  • Operating system and version: Ubuntu 18.04

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-match-statementPython 3.10's match statement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions