Skip to content

Pattern matching a dataclass with different field type fails for Python >= 3.13 #20904

@mtsokol

Description

@mtsokol

Bug Report

Pattern matching on a dataclass with a field of different type than required by the dataclass fails with Cannot determine type of ... for Python 3.13 and higher

To Reproduce

3.12 working gist: https://mypy-play.net/?mypy=latest&python=3.12&gist=f2947a04a9a86defefd16e54c0133c84
3.13 failing gist: https://mypy-play.net/?mypy=latest&python=3.13&gist=f2947a04a9a86defefd16e54c0133c84

from dataclasses import dataclass

@dataclass
class A:
    pass

@dataclass
class B:
    pass

@dataclass
class C:
    sth: A

def func(c: C, b: B):
    match c:
        case C(B() as obj) if obj == b:
            print("matched")

Expected Behavior

I believe it should resolve the type same as for <= 3.12.

Actual Behavior

For Python >= 3.13 inferring obj type fails with:

repro.py:17: error: Cannot determine type of "obj"  [has-type]

Your Environment

  • Mypy version used: 1.19.1
  • Mypy command-line flags: mypy repro.py
  • Mypy configuration options from mypy.ini (and other config files): no additional config
  • Python version used: 3.13.5

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions