This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: dataclass(slots=True) incompatible with __init_subclass__
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: crusaderky, eric.smith
Priority: normal Keywords:

Created on 2022-03-10 00:14 by crusaderky, last changed 2022-04-11 14:59 by admin.

Messages (2)
msg414822 - (view) Author: Guido Imperiale (crusaderky) * Date: 2022-03-10 00:14
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'
msg414823 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-03-10 00:28
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'
History
Date User Action Args
2022-04-11 14:59:57adminsetgithub: 91126
2022-03-10 00:28:03eric.smithsetassignee: eric.smith
type: crash -> behavior
messages: + msg414823
2022-03-10 00:15:08crusaderkysettype: crash
2022-03-10 00:14:48crusaderkysetnosy: + eric.smith
2022-03-10 00:14:28crusaderkycreate