python / python/cpython

Add a new parameter to `@dataclass` to optionally turn off the synthesizing of a `__replace__` method

Aperta
#140,457 5 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stdlib type-feature
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Feature or enhancement

Proposal:

Most (all?) synthesized methods on dataclasses can be turned off:

  • __init__ -> init=False
  • __repr__ -> repr=False
  • __eq__ -> eq=False
  • __match_args__ -> match_args=False
  • __slots__ -> slots=False
  • __lt__(), __le__(), __gt__(), __ge__() -> order=False

But __replace__ cannot be turned off, which presents a problem for generic dataclasses: https://discuss.python.org/t/make-replace-stop-interfering-with-variance-inference/96092/

I propose this

@dataclass(replace=False)
class D:
    x: int

to create a dataclass without __replace__ method.

As far as I can tell, all that's needed is to put a guard around this line:

https://github.com/python/cpython/blob/a4709e525f5bc3a1719b33f52377cccb2af70278/Lib/dataclasses.py#L1098

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

I proposed it in this comment on the DPO thread: https://discuss.python.org/t/make-replace-stop-interfering-with-variance-inference/96092/19

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 in Lib/dataclasses.py, alla riga collegata in cui viene sintetizzato replace, e analizza la gestione dei parametri dataclass circostanti. Il lavoro è completato quando una dataclass con replace=False non riceve replace, mentre il comportamento predefinito rimane invariato; aggiungi o aggiorna i test dataclasses pertinenti secondo necessità.

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

Valutazione

Stack tecnologico
python
Ambito
backend
Tipo di issue
Funzionalità
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
52/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.