44
55import yaml
66
7+ from cycode .logger import get_logger
8+
9+ logger = get_logger ('YAML Utils' )
10+
711
812def _deep_update (source : dict [Hashable , Any ], overrides : dict [Hashable , Any ]) -> dict [Hashable , Any ]:
913 for key , value in overrides .items ():
@@ -15,10 +19,16 @@ def _deep_update(source: dict[Hashable, Any], overrides: dict[Hashable, Any]) ->
1519 return source
1620
1721
18- def _yaml_safe_load (file : TextIO ) -> dict [Hashable , Any ]:
22+ def _yaml_object_safe_load (file : TextIO ) -> dict [Hashable , Any ]:
1923 # loader.get_single_data could return None
2024 loaded_file = yaml .safe_load (file )
21- if loaded_file is None :
25+
26+ if not isinstance (loaded_file , dict ):
27+ # forbid literals at the top level
28+ logger .debug (
29+ 'YAML file does not contain a dictionary at the top level: %s' ,
30+ {'filename' : file .name , 'actual_type' : type (loaded_file )},
31+ )
2232 return {}
2333
2434 return loaded_file
@@ -29,7 +39,7 @@ def read_yaml_file(filename: str) -> dict[Hashable, Any]:
2939 return {}
3040
3141 with open (filename , encoding = 'UTF-8' ) as file :
32- return _yaml_safe_load (file )
42+ return _yaml_object_safe_load (file )
3343
3444
3545def write_yaml_file (filename : str , content : dict [Hashable , Any ]) -> None :
0 commit comments