python / python/mypy

Dataclass field ordering should not be enforced when init=False

Aperta
#10,309 4 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Dataclasses support fields that have default values. All fields with defaults must appear after all fields without default values. Mypy enforces this ordering requirement and emits an error ("Attributes without a default cannot follow attributes with one") if it is violated. Pyright does the same.

By default, dataclasses include a synthesized __init__ method, but this behavior can be overridden by including an init=False argument to the dataclass decorator.

A pyright user recently submitted a bug report indicating that the field order requirement should not be enforced when init=False. I've re-read PEP 557, and it doesn't provide clarity on this point.

Either mypy and pyright are both doing the right thing here or both of them have a bug.

Perhaps someone who was involved in the development of PEP 557, the dataclass implementation, or the mypy dataclass internal plugin could help clarify which behavior is correct?

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 esaminando PEP 557, l’implementazione di dataclass e il plugin interno dataclass di mypy, quindi confronta il comportamento segnalato in pyright issue 1753. Determina se l’errore relativo all’ordine dei campi debba applicarsi quando il decoratore dataclass usa init=False, quindi documenta o implementa il comportamento concordato.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.