Skip to content

Commit 98f3106

Browse files
committed
raise YAMLError if the parsed yaml is not a dict
1 parent a637814 commit 98f3106

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

waveform_editor/annotations.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,17 @@ def add_yaml_error(self, error):
3838
Args:
3939
error: The YAML parsing error to be added as annotations.
4040
"""
41+
message = "Unable to parse the YAML file:\n"
4142
if hasattr(error, "problem_mark"):
4243
line = error.problem_mark.line
4344
# TODO: Is there a way to visualize the column into the annotation? They are
4445
# currently ignored.
4546
# column = error.problem_mark.column
46-
message = f"Unable to parse the YAML file:\n{error.problem}"
47-
48-
self.add(line, message)
47+
message += f"{error.problem}"
4948
else:
50-
self.add(0, f"Unknown YAML error: {error}")
49+
line = 0
50+
message += f"{error}"
51+
self.add(line, message)
5152

5253
def suggest(self, word_to_match, possible_matches):
5354
"""Suggest a close match for a given word from a list of possible matches.

waveform_editor/yaml_parser.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ def parse_waveforms_from_string(self, yaml_str):
3434
self.has_yaml_error = False
3535
try:
3636
waveform_yaml = yaml.load(yaml_str, Loader=LineNumberYamlLoader)
37+
38+
if not isinstance(waveform_yaml, dict):
39+
raise yaml.YAMLError(
40+
f"Expected a dictionary but got {type(waveform_yaml).__name__!r}"
41+
)
42+
3743
waveform = waveform_yaml.get("waveform", [])
3844
self.waveform = Waveform(waveform=waveform)
3945
except yaml.YAMLError as e:

0 commit comments

Comments
 (0)