python / python/mypy

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

Open
#20,904 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-dataclasses topic-match-statement
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the repro.py example and run mypy 1.19.1 under Python 3.12 and 3.13 using the linked playground cases. Trace the pattern-matching and dataclass inference involved in case C(B() as obj); done means Python 3.13 infers obj consistently with 3.12 without the Cannot determine type error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.