I have a question regrading nested data objects.
As suggested in other post (Creating nested dataclass objects in Python), we can use dacite package to create nested data objects. This works on handling the data that we nested dictionary structure.
from dataclasses import dataclass
from dacite import from_dict
@dataclass
class A:
x: str
y: int
@dataclass
class B:
a: A
data = {
'a': {
'x': 'test',
'y': 1,
}
}
result = from_dict(data_class=B, data=data)
assert result == B(a=A(x='test', y=1))
However I have a data type check function and type conversion function associate with the dataclass A in the example. Where I use inspect.signature(A).parameters
to get the expected type of the dataclass and check if data are in the expected type. Can we also call the signature of class A from class B or is there any work around on this? Thanks.
The type current type check code is something like this:
expected_para = inspect.signature(A).parameters
def type_check(d_f: dict, expected_para):
d_new = {}
for k in expected_para.keys():
# get expected type
expected_type = expected_para[k].annotation
# check if k in d_f
if k in d_f.keys():
# check type
v = d_f[k]
#type conversion here
d_new[k] = self.typeconversion(expected_type, v)
else:
d_new[k] = None
return d_new