python / python/cpython

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

オープン
#94,845 コメント 2 件 リアクション 1 件 担当者 1 名 GitHub で見る

@ericvsmith がすでに取り組んでいます。

2022年7月14日 から。

stdlib topic-dataclasses type-bug
主要言語
Python
スター
77.2k
フォーク
36k
PR マージ指標
PR 指標を取得中

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。