问题
I have the following class
@dataclass_json
@dataclass
class Input:
sources: List[Sources] =None
Transformations: List[str] =None
As well as:
@dataclass_json
@dataclass
class Source:
type: str =None
label: str =None
path: str = None
and the two subclasses:
@dataclass_json
@dataclass
class Csv(Source):
csv_path: str=None
delimiter: str=';'
and
@dataclass_json
@dataclass
class Parquet(Source):
parquet_path: str=None
Given now the dictionary:
parquet={type: 'Parquet', label: 'events', path: '/.../test.parquet', parquet_path: '../../result.parquet'}
csv={type: 'Csv', label: 'events', path: '/.../test.csv', csv_path: '../../result.csv', delimiter:','}
input={'sources':[csv, parquet]}
Now I would like to do something like
Input().from_dict(input)
with Output:
Input(sources: [Parquet(...), Csv(...)]).
This actually works but it just returns the values of the 3 paremeters from dataclass Source (type, label and path) and not the other specific parameters of Csv and Parquet (csv_path, delimiter and parquet_path), which are just given its default value. This may be due to the library dataclass_json taken the schema of a class before initializing it. On the other hand, I would like to make still use of dataclass_json and just make a wrapper since it has good case and error handling.
I want this behaviour since each source type has different arguments which defines itself (for example Csv has delimiters but Parquet does not, etc. ...).
I struggled trying to obtain the class Source and find the subclasses in the library dataclass_json. Doing so, I encountered
cls.__args__[0]
which is of type 'GenericMeta'. But given this, I could not obtain access to its subclasses.
Is there any work around?
I am using Python 3.6, by the way.
Thanks in advance for your help.
来源:https://stackoverflow.com/questions/61541259/dictionary-to-dataclasses-with-inheritance-of-classes