python / python/cpython

(🐞) Can't modify attributes and properties on frozen dataclass, has incorrect error message

Aperta
#94,845 2 commenti 1 reazione 1 assegnatario Vedi su GitHub

@ericvsmith ci sta già lavorando.

Dal 14/7/2022.

stdlib topic-dataclasses type-bug
Lingua principale
Python
Stelle
77.2k
Fork
36k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

from dataclasses import dataclass, fields, FrozenInstanceError


@dataclass(frozen=True)
class A:
    a: int = 1
    b = 1
    @property
    def c(self):
        return 1
    @c.setter
    def c(self, value):
        print("suspect")

a = A()
print("fields:", list(field.name for field in fields(a)))
try:
    a.a = 2
except FrozenInstanceError as e:
    print(e)
try:
    a.b = 2
except FrozenInstanceError as e:
    print(e)
try:
    a.c = 2
except FrozenInstanceError as e:
    print(e)
👉 python test.py
fields: ['a']
cannot assign to field 'a'
cannot assign to field 'b'
cannot assign to field 'c'

a is the only field on this class, yet the other attribute and property have the same immutability rules applied to them, and c doesn't even mutate anything.

At the very least, the error message is wrong as these other members aren't fields.

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.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.