python / python/cpython

slotted dataclasses not calling descriptor field `__set__`

オープン
#132,946 コメント 7 件 リアクション 0 件 担当者 1 名 GitHub で見る

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

2025年4月25日 から。

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

説明

Bug report

Bug description:

(Sorry to be finding these funny little dudes)

import dataclasses


class DescriptorFieldType:
    def __set_name__(self, owner, name: str) -> None:
        self.name = name

    def __get__(self, instance, owner):
        if instance is None:
            return None
        return instance.__dict__[self.name]

    def __set__(self, instance, value) -> None:
        raise ValueError("Unsettable")


@dataclasses.dataclass(slots=True)
class Slotted:
    field: DescriptorFieldType = DescriptorFieldType()

Slotted()

@dataclasses.dataclass
class Unslotted:
    field: DescriptorFieldType = DescriptorFieldType()

Unslotted()

Will error with:

Traceback (most recent call last):
  File "[...]", line 27, in <module>
    Unslotted()
  File "<string>", line 3, in __init__
  File "[...]", line 14, in __set__
    raise ValueError("Unsettable")
ValueError: Unsettable

(errors on Unslotted but not Slotted)

CPython versions tested on:

3.11

Operating systems tested on:

No response

Linked PRs
  • gh-133002

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

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

はじめの一歩

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

評価

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

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

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