Skip to content

Commit

Permalink
Treat arrays with empty shapes as numbers
Browse files Browse the repository at this point in the history
Calling `tolist()` on these types of arrays yields a single number
rather than a list. This would cause errors with the yaml dumper since
it was expected a list.

Instead, we should just follow the route for single numbers and call
`.item()` instead. This fixes the issue.

Signed-off-by: Patrick Avery <[email protected]>
  • Loading branch information
psavery committed Aug 18, 2024
1 parent cb5b114 commit 47d5616
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions hexrd/utils/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ class NumpyToNativeDumper(yaml.SafeDumper):
converted to a basic type.
"""
def represent_data(self, data):
if isinstance(data, np.ndarray):
# Empty shape arrays should be treated as numbers, not arrays.
is_empty_shape_array = (
isinstance(data, np.ndarray) and data.shape == ()
)
if isinstance(data, np.ndarray) and not is_empty_shape_array:
return self.represent_list(data.tolist())
elif isinstance(data, (np.generic, np.number)):
elif isinstance(data, (np.generic, np.number)) or is_empty_shape_array:
item = data.item()
if isinstance(item, (np.generic, np.number)):
# This means it was not converted successfully.
Expand Down

0 comments on commit 47d5616

Please sign in to comment.