|
The CaseTalkBaseClass is added with metadata support for constraints:
#
# Base class to support checking constraints through metadata.
#
@dataclass
class CaseTalkBaseClass:
def __post_init__(self):
for fieldname, field in self.__dataclass_fields__.items():
if 'required' in field.metadata:
if field.metadata['required'] == True and getattr(self, fieldname) is none:
raise ValueError('{fieldname} is Required')
def __eq__(self, other):
result = None
for fieldname, field in self.__dataclass_fields__.items():
if 'id' in field.metadata:
if field.metadata['id'] == True:
result = cmp(getattr(self, fieldname), getattr(other, fieldname))
if result <> 0:
break
return result
@dataclass
class Student(CaseTalkBaseClass):
firstname: str = field(metadata={'required'=True, 'id'=True})
lastname: str = field(metadata={'required'=True, 'id'=True})
buddy: Student
type: Type = field(default="Student")
|