Pattern matching a dataclass with different field type fails for Python >= 3.13
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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