22from os import PathLike
33from typing import TextIO , Union
44
5- from pydantic_yaml import parse_yaml_raw_as
5+ import yaml
66
77from harp .model import Model , Registers
88
99
10+ def _convert_keys_to_strings (obj ):
11+ """Recursively converts all dictionary keys to strings.
12+ This is necessary since pydantic deserialization from python objects
13+ seems to expect keys to always be strings."""
14+ if isinstance (obj , dict ):
15+ return {str (k ): _convert_keys_to_strings (v ) for k , v in obj .items ()}
16+ elif isinstance (obj , list ):
17+ return [_convert_keys_to_strings (i ) for i in obj ]
18+ return obj
19+
20+
1021def _read_common_registers () -> Registers :
1122 if __package__ is None :
1223 raise ValueError ("__package__ is None: unable to read common registers" )
1324
1425 file = resources .files (__package__ ) / "common.yml"
1526 with file .open ("r" ) as fileIO :
16- return parse_yaml_raw_as (Registers , fileIO .read ())
27+ regs_raw = _convert_keys_to_strings (yaml .safe_load (fileIO .read ()))
28+ return Registers .model_validate (regs_raw )
1729
1830
1931def read_schema (file : Union [str , PathLike , TextIO ], include_common_registers : bool = True ) -> Model :
@@ -37,7 +49,8 @@ def read_schema(file: Union[str, PathLike, TextIO], include_common_registers: bo
3749 with open (file ) as fileIO :
3850 return read_schema (fileIO )
3951 else :
40- schema = parse_yaml_raw_as (Model , file .read ())
52+ schema_raw = _convert_keys_to_strings (yaml .safe_load (file .read ()))
53+ schema = Model .model_validate (schema_raw )
4154 if "WhoAmI" not in schema .registers and include_common_registers :
4255 common = _read_common_registers ()
4356 schema .registers = dict (common .registers , ** schema .registers )
0 commit comments