Package: bentoudev.dataclass
Bentoudev.dataclassYaml to dataclass loader. Validates objects based on type information.Supports folowing types:classes marked as dataclass (fromdataclasses)int, str, float, listEnum (fromenum)Optional, List, Dict, Union (fromtyping)forward references to not yet known types (see example), including self-referencingInstallpipinstallbentoudev.dataclassDocumentationWork in progress, for now, check out examples below or browse the source code.Example@dataclassclassPerson:name:strage:intmoney:floatyaml_content=('name: John\n''age: 30\n''money: 400.50')obj=load_yaml_dataclass(Person,'Person',yaml_content)assertobj.name=='John'Inline loadersIf you need to load complex class from a single value (like string), you can use@inline_loaderattributeimportbentoudev.dataclass.yaml_loaderimportbentoudev.dataclass.base@dataclass@inline_loader(source_type=str,field_name='name')classObjFromStr:name:strfoo:intbar:float@dataclassclassContainer:value:ObjFromStrobj=load_yaml_dataclass(Container,'pretty file name','value: ThisIsMyName')assertobj.value.name=='ThisIsMyName'Forward references to external typesSometimes you might want to load dataclass that forward references foreign types, from other modules, in form of a string. In order to support such types, loader must be supplied with list of them.@dataclassclassMyDataclass:foo:Optional['my_namespace.project.model.my_ext_dataclass']local_types=base.get_types_from_modules([__name__,'my_namespace.project.model.my_ext_dataclass'])my_obj:MyDataclass=yaml_loader.load_yaml_dataclass(MyDataclass,'pretty file name',yaml_content,ext_types=local_types)Self referencing typesAdditionaly to external types, self referencing is also supportedfromdataclassesimportdataclassimportbentoudev.dataclass.yaml_loaderasyaml_loader@dataclassclassMyDataclass:my_string:strself_nested:Optional['MyDataclass']list_of_sth:List[str]user_data:Dict[str,str]yaml_content=('my_string: foo\n''self_nested:\n''  my_string: bar\n''  list_of_sth: inline_value\n''list_of_sth:\n''- first\n''- second\n''user_data:\n''  anything: goes')my_obj:MyDataclass=yaml_loader.load_yaml_dataclass(MyDataclass,'pretty file name',yaml_content)Remember linesAdditional information about source from which obj/field was loaded can be enabled by using@track_sourceattribute, or settingalways_track_sourceparameter to True (disabled by default, but recomended). Such information is then used to print prettier errors inDataclassLoadError.classEKind(Enum):FIRST=1SECOND=2@dataclassclassSomeClass:kind:EKindtry:obj=yaml_loader.load_yaml_dataclass(SomeClass,'[SomeClass] my_file.yml','kind: THIRD',always_track_source=True)exceptDataclassLoadErroraserr:print(err)Outputs:error: Got 'THIRD' when expecting enum 'EKind' with one of values: FIRST, SECOND
in "[SomeClass] my_file.yml", line 1, column 1:
kind: THIRD
^ (line: 1)If you desire to retrieve this information and print error yourself, access it'ssourcefield in error, or use injected methodsget_root_sourceorget_field_source.try:obj=yaml_loader.load_yaml_dataclass(SomeClass,'broken_file.yml',broken_yaml_content,always_track_source=True)field_src=obj.get_field_source('my_field_name')print(f"Value location line '{field_src.line_number}', column '{field_src.column_number}'")exceptDataclassLoadErroraserr:print(f"Error location line '{err.source.line_number}', column '{err.source.column_number}'")Additionaly, you can control how many lines are loaded for code snippet and in which format line numbers are presented viaerror_code_snippet_linesanderror_format(Pretty or MSVC compatible).