slotted dataclasses not calling descriptor field `__set__`
Open
@sobolevn is already working on this.
Since Apr 25, 2025.
stdlib
topic-dataclasses
type-bug
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.