python / python/mypy

Issue initialising dataclass with getters and setters

Aperta
#9,779 5 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

When using mypy with a dataclass that includes getters and setters it throws an error. It returns Name 'byr' already defined on line 5.

To Reproduce

To reproduce use mypy with this script:

@dataclass
class Passport:
    byr: int

    @property
    def byr(self) -> int:
        return self._byr

    @byr.setter
    def byr(self, value) -> None:
        if int(value) < 1920 or int(value) > 2002:
            raise Exception("Birth year must be between 1920 and 2002")
        self._byr = int(value)

Expected Behavior

I would have expected it to accept this, as this seems to be a standard way to handle getters and setters.

Your Environment

  • Mypy command-line flags: python -m mypy my.py
  • Python version used: 3.9.0
  • Operating system and version:

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 la dataclass fornita ed esegui python -m mypy my.py per riprodurre l'errore di nome duplicato. Traccia come vengono analizzate le dataclasses e i property getters/setters, quindi aggiungi un test di regressione che mostri che questo schema viene accettato senza l'errore.

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
28/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.