Since dataclass._add_slots
gets its namespace from the __dict__
of the current class:
I solved the problem by propagating __classcell__
as a class attribute for dataclass
to create a namespace with, and deleting it afterwards:
_dataclass_keywords = set((code := dataclass.__code__).co_varnames[
code.co_argcount:code.co_argcount + code.co_kwonlyargcount])
@dataclass_transform()
class DataclassMeta(type):
def __new__(metacls, name, bases, namespace, **kwargs):
class InitSubclassBlocker:
def __init_subclass__(cls, **kwargs):
pass
configured_dataclass = dataclass(**{key: kwargs.pop(key)
for key in kwargs.keys() & _dataclass_keywords})
cls = super().__new__(
metacls, name, (InitSubclassBlocker,) + bases, namespace, **kwargs)
if classcell := namespace.get('__classcell__'):
cls.__classcell__ = classcell
cls = configured_dataclass(cls)
if hasattr(cls, '__classcell__'):
del cls.__classcell__
del InitSubclassBlocker.__init_subclass__
super(cls, cls).__init_subclass__(**kwargs)
return cls
Demo here