python / python/cpython

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

Open
#94,845 2 comments 1 reaction 1 assignee View on GitHub

@ericvsmith is already working on this.

Since Jul 14, 2022.

stdlib topic-dataclasses type-bug
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.