python / python/mypy

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

Aperta
#20,904 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-dataclasses topic-match-statement
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con l’esempio repro.py ed esegui mypy 1.19.1 con Python 3.12 e 3.13 usando i casi del playground collegati. Traccia il pattern matching e l’inferenza di dataclass coinvolti in case C(B() as obj); il lavoro è concluso quando Python 3.13 inferisce obj in modo coerente con 3.12 senza l’errore Cannot determine type.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.