Issue 46970: dataclass(slots=True) incompatible with init_subclass (original) (raw)

Related to #46382 A class decorated with dataclass(slots=True) can't pass any parameters to the init_subclass method of its parent class.

from dataclasses import dataclass

class A: slots = () def init_subclass(cls, msg): print(msg)

@dataclass(slots=True) class B(A, msg="Hello world!"): pass

File "lib/python3.10/dataclasses.py", line 1145, in _add_slots cls = type(cls)(cls.name, cls.bases, cls_dict) TypeError: A.init_subclass() missing 1 required positional argument: 'msg'

This appears to be due to dataclasses needing to create a new class in order to set slots. I'll look at it, but I doubt there's anything that can be done.

attrs has the same issue: File "xxxxx/.local/lib/python3.8/site-packages/attr/_make.py", line 889, in _create_slots_class cls = type(self._cls)(self._cls.name, self._cls.bases, cd) TypeError: init_subclass() missing 1 required positional argument: 'msg'