@dataclass
class A:
literal: StrLiteral('a')
@dataclass
class B:
literal: StrLiteral('b')
@dataclass
class C:
a_or_b: Union[A, B]
C.to_dict({'literal': 'a'}) and C.to_dict({'literal': 'b'}) should instantiate the right subclass for a_or_b.
voluptuous does this by checking all of the possible subschemas and picking the one that matches, or raises an exception if none of them match. we may want to implement something similar in to_dict
another approach would be to leverage json schema's if/else construct to specify different subschemas based on a switch field.
given dataclasses like:
C.to_dict({'literal': 'a'})
andC.to_dict({'literal': 'b'})
should instantiate the right subclass fora_or_b
.voluptuous does this by checking all of the possible subschemas and picking the one that matches, or raises an exception if none of them match. we may want to implement something similar in
to_dict
another approach would be to leverage json schema's
if/else
construct to specify different subschemas based on a switch field.