Dataclass constructors defaults don't solve type parameters

Aperta
#5,601 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
57/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python, rust

Direzione di ricerca

Start by reading the generated dataclass constructor logic and the generic argument solving path in Pyrefly; the issue does not name specific source files or tests. Use the Python example in the issue as a regression case: Generated() should reveal Generated[int], while Generated(5) should continue to reveal Generated[int]. Add or update a focused type-checker test for those results.

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

Descrizione

typechecking
Describe the Bug

Consider this code:

from dataclasses import dataclass
from typing import reveal_type

class Explicit[T]:
    def __init__(self, x: T = 5) -> None:
        self.x = x

@dataclass
class Generated[T]:
    x: T = 5  # pyrefly: ignore[bad-assignment]

reveal_type(Explicit())    # Explicit[int]
reveal_type(Generated())   # Generated[@_] — expected Generated[int]
reveal_type(Generated(5))  # Generated[int]

There's a gap in our generated dataclass constructor logic. The default value for x is not actually affecting how calls solve. It's clear from Explicit that we intend for default constructor arguments to solve generic tparams when suitable, but in Generated we ignore the fact that the default of 5 means T ought to solve to int.

Note that it's possible to make this example work with T = int, a tparam-level default. But that's not a good solution - as a rule we don't want to use tparam-level defaults (which are an advanced feature and have some probably-undesirable side effects, like making a bare Generated mean Generated[int] rather than Generated[Any]) for a case where they aren't necessary

Sandbox Link

https://pyrefly.org/sandbox/?project=v2.pVUxEoIwEPzKzaQACk1FQ-UTLOiQcSJkkBkFRqKT0kf4Ql_iJSGSqKSxhOxtLrndTTAyQ7afksFP1Y8osn4bTm3ViiIvlz0nM8jxgOmi3SSuyt8poLOa4Y3OO1g6AALTlDIwN1wcWL3CMvw6807oYHD6jm27cZIoJjL3j-pCtIt972zAxGllsy_heX8AlwMOmdfOUognVUQujwJ_S43A1vyAaWoYsqjR5opFqL31riOY4hyOQgxjRqkt7y8N5R2t-2qkXgUF5Sp2Y61-IHw66LU1RqT9R-ORGUAUUnikBI6IoGhf

(Only applicable for extension issues) IDE Information

No response

Lingua principale
Rust
Stelle
7.2k
Fork
578
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

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.

Altre issue di facebook/pyrefly

Tutte le issue di facebook/pyrefly

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.