python / python/cpython

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

Offen
#94,845 2 Kommentare 1 Reaktion 1 zugewiesene Person Auf GitHub ansehen

@ericvsmith arbeitet bereits daran.

Seit 14.7.2022.

stdlib topic-dataclasses type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
36k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.