python / python/cpython

slotted dataclasses not calling descriptor field `__set__`

Đang mở
#132,946 7 bình luận 0 reaction 1 người được giao Xem trên GitHub

@sobolevn đang làm issue này rồi.

Từ ngày 25/4/2025.

stdlib topic-dataclasses type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.