python / python/mypy

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

Offen
#20,904 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-dataclasses topic-match-statement
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem Beispiel repro.py und führe mypy 1.19.1 unter Python 3.12 und 3.13 anhand der verlinkten Playground-Fälle aus. Verfolge die an case C(B() as obj) beteiligte Mustererkennung und Dataclass-Inferenz; abgeschlossen ist die Aufgabe, wenn Python 3.13 obj konsistent mit 3.12 inferiert, ohne den Fehler Cannot determine type.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.