I have a super dataclass (Settings) and multiple children who inherit the settings of the super dataclass (FSettings, BSettings).
How can I pass a dictionary of "arguments" to a child dataclass in a way where it sets them, if applicable?
from dataclasses import dataclass
@dataclass()
class Settings:
verbose: bool
detailed: bool
@dataclass()
class FSettings(Settings):
vals: list
def __init__(self, args):
for arg in args:
if arg in list(self.__annotations__):
self.__setattr__(arg, args[arg])
if __name__ == '__main__':
args = {'vals': ["a", "b"], 'verbose': True, 'detailed':False}
s = FSettings(args)
print(s)
If I print list(self.__annotations__)
it shows ['vals']
, ignoring verbose
and detailed