Skip to content

Commit

Permalink
yaml_helper: improve errror handling and typing hints
Browse files Browse the repository at this point in the history
  • Loading branch information
hagau committed Sep 28, 2024
1 parent 3adca19 commit d0765d2
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions yaml_helper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Callable, Iterable
from typing import Callable, Iterable, Union

import yaml

Expand Down Expand Up @@ -40,7 +40,7 @@ def construct_numeral(loader, node, type_constructor:Callable = int):
x = type_constructor(x)
return x

def construct_joined_sequence(loader, sequence):
def construct_joined_sequence(loader:yaml.Loader, sequence:Union[yaml.nodes.SequenceNode, Iterable]):
if isinstance(sequence, yaml.nodes.SequenceNode):
# Construct the nodes of the SequenceNode into an Iterable.
joined_sequence = decode_node(loader, sequence)
Expand All @@ -66,7 +66,12 @@ def construct_joined_sequence(loader, sequence):
for elm in nodes_to_join:
joined_sequence.update(elm)
else:
raise TypeError(f'Unsuitable types for joining: {nodes_to_join}')
# Inhomogenous sequences are not supported.
raise TypeError(f"Unsuitable types for joining: {[ type(x).__name__ for x in nodes_to_join ]}"
f"\n{nodes_to_join= }")
else:
# Cover all remaining cases.
raise TypeError(f"Unsuitable sequence for joining: {type(sequence).__name__}" f"\n{sequence}")

return joined_sequence

Expand Down

0 comments on commit d0765d2

Please sign in to comment.