Language: Python · Versions: Python 3.13
The problem
I have a base @dataclass(slots=True) and a derived @dataclass(slots=True) that overrides a field with a default value or uses a custom data descriptor. When @dataclass(slots=True) generates the new class in Python, it creates a brand-new class object (replacing the original class after body execution). As a result:
1. Any custom descriptor or method that captured the original class object during class creation (or a zero-arg super() reference inside a closure created before @dataclass(slots=True) rebuilt the class) points to the pre-slot class rather than the slotted class, causing TypeError: super(type, obj): obj must be an instance or subtype of type!
2. If a parent dataclass already defines __slots__ and a child dataclass repeats a field name in its annotations, TypeError: 'x' in __slots__ conflicts with class variable or duplicate slot descriptor masking occurs.
Minimal example
from dataclasses import dataclass
def make_getter():
class Base:
def greet(self) -> str:
return "base"
@dataclass(slots=True)
class Child(Base):
x: int = 1
# A closure created before @dataclass(slots=True) replaces Child:
def greet(self) -> str:
return super().greet() + f":{self.x}"
return Child
c = make_getter()(42)
print(c.greet())
Expected
understand how @dataclass(slots=True) rebinds the __class__ cell for super() and why descriptors or metaclasses that cache cls during __set_name__ break when slots=True reconstructs the class.
What happens instead
When a custom descriptor or metaclass stores owner inside __set_name__(self, owner, name), owner is the throwaway unslotted class created before @dataclass(slots=True) allocates the replacement slotted type(cls.__name__, cls.__bases__, ...)—so isinstance(instance, self.owner) returns False at runtime!
How do you write a descriptor or class decorator that remains valid when combined with @dataclass(slots=True) in Python 3.13?
Parts of the premise hold and parts don't. This is from reading CPython's dataclasses._add_slots and the dataclass docs. I haven't run it.
What holds
slots=True calls type(cls)(cls.__name__, cls.__bases__, cls_dict), so the decorator receives the original class and returns a different class object. Zero-arg super() reads the __class__ cell, which still points at the old class. self is an instance of the new class, which is not a subclass of the old one, so you get your TypeError: super(type, obj): obj must be an instance or subtype of type. The slots docs mention this for 3.13 and earlier. From memory, 3.14 fixed it (gh-90562) by rewriting the __class__ cell in the class's functions. Check the 3.14 changelog if you depend on that.
The minimal fix on 3.13 is two-arg super:
@dataclass(slots=True)
class Child(Base):
x: int = 1
def greet(self) -> str:
return super(Child, self).greet() + f":{self.x}"
Child is looked up when the method runs, so it resolves to the decorated class. That works for module-level and closure-local names alike.
What doesn't hold
__set_name__is called twice, not once.type.__new__calls it on every attribute in the namespace. That happens once for the original class and again for the slotted one. A descriptor that doesself.owner = ownerunconditionally ends up with the slotted class, because the second call overwrites the first. The stale owner only appears if you guard it (if self.owner is None) or if the descriptor was removed from the class dict.- Descriptors used as field defaults are removed.
_add_slotspops each field's default fromcls_dict, because a slot and a class attribute of the same name conflict. A descriptor assigned asx: int = MyDesc()is therefore never re-registered on the new class. It keeps the old owner, which is your real bug. - The inherited-slots conflict you describe shouldn't happen between dataclasses.
_add_slotscollects slots from the base classes and leaves them out of the child's__slots__. The'x' in __slots__ conflicts with class variableerror comes from hand-written__slots__plus a default.slots=Trueitself raises if__slots__is already defined.
A descriptor that survives this
Don't cache the owner. Use the objtype that __get__ already receives, or type(obj):
class Checked:
def __set_name__(self, owner, name):
self.name = name # idempotent; never store owner
def __get__(self, obj, objtype=None):
if obj is None:
return self
return obj.__dict__.get(self.name) # or a slot-backed store
def __set__(self, obj, value):
if not isinstance(value, int):
raise TypeError(type(obj).__name__)
obj.__dict__[self.name] = value
Two caveats. __dict__ doesn't exist on a slotted class, so back the value with a differently named slot such as _x. And a field default that is a descriptor is dropped from the class dict, so declare it with field(default=...) or attach it after the decorator runs.
For a class decorator, apply it outside @dataclass(slots=True), so it sees the final class. If it must run first, don't keep references to the class. Use __init_subclass__ or look the class up lazily.
To settle point 1 on your own machine, print id(owner) inside __set_name__ for a plain descriptor attribute and count how many times it prints.