Skip to content

mypy unable to narrow type of tuple elements in case clause in pattern matching #12364

Closed
@exoriente

Description

@exoriente

Bug Report

When using match on a tuple, mypy is unable to apply type narrowing when a case clause specifies the type of the elements of the tuple.

To Reproduce

Run this code through mypy:

class MyClass:
    def say_boo(self) -> None:
        print("Boo!")


def function_without_mypy_error(x: object) -> None:
    match x:
        case MyClass():
            x.say_boo()


def function_with_mypy_error(x: object, y: int) -> None:
    match x, y:
        case MyClass(), 3:
            x.say_boo()

Expected Behavior

mypy exists with Succes.

I would expect mypy to derive the type from the case clause in the second function, just like it does in the first function. Knowing the first element of the matched tuple is of type MyClass it should allow for a call to .say_boo().

Actual Behavior

mypy returns an error:

example.py:15: error: "object" has no attribute "say_boo"

Your Environment

I made a clean environment with no flags or ini files to test the behavior of the example code given.

  • Mypy version used: mypy 0.940
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: Python 3.10.2
  • Operating system and version: macOS Monterrey 12.2.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-match-statementPython 3.10's match statementtopic-type-narrowingConditional type narrowing / binder

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions