Skip to content

Commit 477f8cf

Browse files
dcheriankeewis
andauthored
Better attr diff for testing.assert_identical (#8400)
* [WIP] Better attr diff * Fix test * Update xarray/core/formatting.py Co-authored-by: Justus Magin <[email protected]> * Improvements --------- Co-authored-by: Justus Magin <[email protected]>
1 parent 83fbcf0 commit 477f8cf

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

xarray/core/formatting.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -783,9 +783,11 @@ def extra_items_repr(extra_keys, mapping, ab_side, kwargs):
783783
try:
784784
# compare xarray variable
785785
if not callable(compat):
786-
compatible = getattr(a_mapping[k], compat)(b_mapping[k])
786+
compatible = getattr(a_mapping[k].variable, compat)(
787+
b_mapping[k].variable
788+
)
787789
else:
788-
compatible = compat(a_mapping[k], b_mapping[k])
790+
compatible = compat(a_mapping[k].variable, b_mapping[k].variable)
789791
is_variable = True
790792
except AttributeError:
791793
# compare attribute value
@@ -804,18 +806,40 @@ def extra_items_repr(extra_keys, mapping, ab_side, kwargs):
804806

805807
if compat == "identical" and is_variable:
806808
attrs_summary = []
809+
a_attrs = a_mapping[k].attrs
810+
b_attrs = b_mapping[k].attrs
807811

812+
attrs_to_print = set(a_attrs) ^ set(b_attrs)
813+
attrs_to_print.update(
814+
{k for k in set(a_attrs) & set(b_attrs) if a_attrs[k] != b_attrs[k]}
815+
)
808816
for m in (a_mapping, b_mapping):
809817
attr_s = "\n".join(
810-
summarize_attr(ak, av) for ak, av in m[k].attrs.items()
818+
" " + summarize_attr(ak, av)
819+
for ak, av in m[k].attrs.items()
820+
if ak in attrs_to_print
811821
)
822+
if attr_s:
823+
attr_s = " Differing variable attributes:\n" + attr_s
812824
attrs_summary.append(attr_s)
813825

814826
temp = [
815827
"\n".join([var_s, attr_s]) if attr_s else var_s
816828
for var_s, attr_s in zip(temp, attrs_summary)
817829
]
818830

831+
# TODO: It should be possible recursively use _diff_mapping_repr
832+
# instead of explicitly handling variable attrs specially.
833+
# That would require some refactoring.
834+
# newdiff = _diff_mapping_repr(
835+
# {k: v for k,v in a_attrs.items() if k in attrs_to_print},
836+
# {k: v for k,v in b_attrs.items() if k in attrs_to_print},
837+
# compat=compat,
838+
# summarizer=summarize_attr,
839+
# title="Variable Attributes"
840+
# )
841+
# temp += [newdiff]
842+
819843
diff_items += [ab_side + s[1:] for ab_side, s in zip(("L", "R"), temp)]
820844

821845
if diff_items:

xarray/tests/test_formatting.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -406,19 +406,27 @@ def test_diff_dataset_repr(self) -> None:
406406
"var2": ("x", np.array([3, 4], dtype="int64")),
407407
},
408408
coords={
409-
"x": np.array(["a", "b"], dtype="U1"),
409+
"x": (
410+
"x",
411+
np.array(["a", "b"], dtype="U1"),
412+
{"foo": "bar", "same": "same"},
413+
),
410414
"y": np.array([1, 2, 3], dtype="int64"),
411415
},
412-
attrs={"units": "m", "description": "desc"},
416+
attrs={"title": "mytitle", "description": "desc"},
413417
)
414418

415419
ds_b = xr.Dataset(
416420
data_vars={"var1": ("x", np.array([1, 2], dtype="int64"))},
417421
coords={
418-
"x": ("x", np.array(["a", "c"], dtype="U1"), {"source": 0}),
422+
"x": (
423+
"x",
424+
np.array(["a", "c"], dtype="U1"),
425+
{"source": 0, "foo": "baz", "same": "same"},
426+
),
419427
"label": ("x", np.array([1, 2], dtype="int64")),
420428
},
421-
attrs={"units": "kg"},
429+
attrs={"title": "newtitle"},
422430
)
423431

424432
byteorder = "<" if sys.byteorder == "little" else ">"
@@ -429,8 +437,12 @@ def test_diff_dataset_repr(self) -> None:
429437
(x: 2, y: 3) != (x: 2)
430438
Differing coordinates:
431439
L * x (x) %cU1 'a' 'b'
440+
Differing variable attributes:
441+
foo: bar
432442
R * x (x) %cU1 'a' 'c'
433-
source: 0
443+
Differing variable attributes:
444+
source: 0
445+
foo: baz
434446
Coordinates only on the left object:
435447
* y (y) int64 1 2 3
436448
Coordinates only on the right object:
@@ -441,8 +453,8 @@ def test_diff_dataset_repr(self) -> None:
441453
Data variables only on the left object:
442454
var2 (x) int64 3 4
443455
Differing attributes:
444-
L units: m
445-
R units: kg
456+
L title: mytitle
457+
R title: newtitle
446458
Attributes only on the left object:
447459
description: desc"""
448460
% (byteorder, byteorder)

0 commit comments

Comments
 (0)