◐ Shell
clean mode source ↗

Issue 46970: dataclass(slots=True) incompatible with __init_subclass__

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'