I want to make a dataclass base class where all the fields in subclasses are automatically Optional and default to None (if no default is provided).
The following code... almost seems to do what I want, but not quite. It errors out the same way as though I'd never written the __init_subclass__ (i.e. complains about unfilled parameters)... perhaps because my code is running after the dataclass magic happens?
@dataclass(order=True, frozen=True)
class BaseDictKey:
def __init_subclass__(cls, *args, **kwargs):
super().__init_subclass__(*args, **kwargs)
# noinspection PyUnresolvedReferences
for field in cls.__dataclass_fields__.values():
field.default = None if field.default is None else field.default
field.type = typing.Union[field.type, NoneType]
@dataclass(order=True, frozen=True)
class ScoreDictKey(BaseDictKey):
catalog: str # magically becomes catalog: Optional[str] = None
dataset: str = 'foo' # magically becomes dataset: Optional[str] = 'foo'
(In case you're wondering why I want this, I have another baseclass that uses these BaseDictKeys that expects any and all fields in subclasses to be Optional. I suppose I could instead raise an exception if I detect something isn't an Optional, but that seems uglier.)
Is this possible in Python 3.7+?