I have a base class to provide functionality to a variety of classes, and I want to make sure classes that inherit from this one are decorated with @dataclass
. The following definition results in the ValueError
being raised:
from dataclasses import dataclass, is_dataclass
class Base:
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
if not is_dataclass(cls):
raise ValueError('Base subclasses should be dataclasses.')
@dataclass
class Child(Base):
...
As I understand, this is due to the inheritance happening (and therefore Base.__init_subclass__(Child)
running) before the @dataclass
has a chance to run. Is there a nice way to work around this?