diff --git a/CHANGES.rst b/CHANGES.rst index bac63847d..9283e4157 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,7 +4,7 @@ The ASDF Standard is at v1.6.0 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- +- Fix bug in ``asdftool diff`` for arrays within a list [#1672] 3.0.0 (2023-10-16) ------------------ diff --git a/asdf/_tests/commands/tests/data/blocks.diff b/asdf/_tests/commands/tests/data/blocks.diff index 9f04a8167..da3edd2df 100644 --- a/asdf/_tests/commands/tests/data/blocks.diff +++ b/asdf/_tests/commands/tests/data/blocks.diff @@ -10,4 +10,5 @@ tree: - > 9000 < 10000 - ndarrays at "foobar" differ by shape, datatype and contents +> ndarrays differ by shape, datatype and contents +< ndarrays differ by shape, datatype and contents diff --git a/asdf/_tests/commands/tests/data/ndarray_in_list.diff b/asdf/_tests/commands/tests/data/ndarray_in_list.diff new file mode 100644 index 000000000..3a58bce13 --- /dev/null +++ b/asdf/_tests/commands/tests/data/ndarray_in_list.diff @@ -0,0 +1,9 @@ +< tree: +< list: +< - a: +< offset: +< 16 +< strides: +< - -8 +> ndarrays differ by contents +< ndarrays differ by contents diff --git a/asdf/_tests/commands/tests/data/ndarray_in_list0.asdf b/asdf/_tests/commands/tests/data/ndarray_in_list0.asdf new file mode 100644 index 000000000..0b2e1925a Binary files /dev/null and b/asdf/_tests/commands/tests/data/ndarray_in_list0.asdf differ diff --git a/asdf/_tests/commands/tests/data/ndarray_in_list1.asdf b/asdf/_tests/commands/tests/data/ndarray_in_list1.asdf new file mode 100644 index 000000000..90b03ace7 Binary files /dev/null and b/asdf/_tests/commands/tests/data/ndarray_in_list1.asdf differ diff --git a/asdf/_tests/commands/tests/data/ndarrays.diff b/asdf/_tests/commands/tests/data/ndarrays.diff index 5e400cacb..e64165b96 100644 --- a/asdf/_tests/commands/tests/data/ndarrays.diff +++ b/asdf/_tests/commands/tests/data/ndarrays.diff @@ -1 +1,4 @@ - ndarrays at "a" differ by contents +tree: + a: +> ndarrays differ by contents +< ndarrays differ by contents diff --git a/asdf/_tests/commands/tests/data/simple_inline_array.diff b/asdf/_tests/commands/tests/data/simple_inline_array.diff index 3e30e30c1..8a423c5fd 100644 --- a/asdf/_tests/commands/tests/data/simple_inline_array.diff +++ b/asdf/_tests/commands/tests/data/simple_inline_array.diff @@ -1 +1,4 @@ - ndarrays at "array" differ by contents +tree: + array: +> ndarrays differ by contents +< ndarrays differ by contents diff --git a/asdf/_tests/commands/tests/test_diff.py b/asdf/_tests/commands/tests/test_diff.py index d96eb6849..45df7d036 100644 --- a/asdf/_tests/commands/tests/test_diff.py +++ b/asdf/_tests/commands/tests/test_diff.py @@ -54,6 +54,12 @@ def test_diff_ndarray(): _assert_diffs_equal(filenames, result_file, minimal=False) +def test_diff_ndarray_in_list(): + filenames = ["ndarray_in_list0.asdf", "ndarray_in_list1.asdf"] + result_file = "ndarray_in_list.diff" + _assert_diffs_equal(filenames, result_file, minimal=False) + + def test_diff_block(): filenames = ["block0.asdf", "block1.asdf"] result_file = "blocks.diff" diff --git a/asdf/commands/diff.py b/asdf/commands/diff.py index fa8776ad7..7c682fa74 100644 --- a/asdf/commands/diff.py +++ b/asdf/commands/diff.py @@ -104,8 +104,9 @@ class ArrayNode: of but not necessarily display. This allows the diff output to be cleaner.""" - def __init__(self, name): + def __init__(self, name, index): self.name = name + self.index = index def __hash__(self): return hash(self.name) @@ -191,7 +192,7 @@ def print_in_tree(diff_ctx, node_list, thing, other, use_marker=False, last_was_ # If tree element is list, recursively print list contents if isinstance(thing, list): for i, subthing in enumerate(thing): - key = ArrayNode(f"{node_list[-1]}_{i}") + key = ArrayNode(f"{node_list[-1]}_{i}", i) last_was_list = print_in_tree( diff_ctx, [*node_list, key], @@ -277,9 +278,9 @@ def compare_ndarrays(diff_ctx, array0, array1, keys): differences.append("contents") if differences: - prefix = " " * (len(keys) + 1) - msg = f"ndarrays at \"{'.'.join(keys)}\" differ by {human_list(differences)}" - diff_ctx.iostream.write(prefix + RED + msg + RESET_NEWLINE) + msg = f"ndarrays differ by {human_list(differences)}" + print_in_tree(diff_ctx, keys, msg, False, ignore_lwl=True) + print_in_tree(diff_ctx, keys, msg, True, ignore_lwl=True) def both_are_ndarrays(tree0, tree1): @@ -321,7 +322,7 @@ def compare_trees(diff_ctx, tree0, tree1, keys=None): compare_dicts(diff_ctx, tree0, tree1, keys) elif isinstance(tree0, list) and isinstance(tree1, list): for i, (obj0, obj1) in enumerate(zip(tree0, tree1)): - key = ArrayNode(f"item_{i}") + key = ArrayNode(f"item_{i}", i) compare_trees(diff_ctx, obj0, obj1, [*keys, key]) else: compare_objects(diff_ctx, tree0, tree1, keys)