Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better attr diff for testing.assert_identical #8400

Merged
merged 5 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,9 +783,11 @@ def extra_items_repr(extra_keys, mapping, ab_side, kwargs):
try:
# compare xarray variable
if not callable(compat):
compatible = getattr(a_mapping[k], compat)(b_mapping[k])
compatible = getattr(a_mapping[k].variable, compat)(
b_mapping[k].variable
)
else:
compatible = compat(a_mapping[k], b_mapping[k])
compatible = compat(a_mapping[k].variable, b_mapping[k].variable)
is_variable = True
except AttributeError:
# compare attribute value
Expand All @@ -804,18 +806,40 @@ def extra_items_repr(extra_keys, mapping, ab_side, kwargs):

if compat == "identical" and is_variable:
attrs_summary = []
a_attrs = a_mapping[k].attrs
b_attrs = b_mapping[k].attrs

attrs_to_print = set(a_attrs) ^ set(b_attrs)
attrs_to_print.update(
{k for k in set(a_attrs) & set(b_attrs) if a_attrs[k] != b_attrs[k]}
)
for m in (a_mapping, b_mapping):
attr_s = "\n".join(
summarize_attr(ak, av) for ak, av in m[k].attrs.items()
" " + summarize_attr(ak, av)
for ak, av in m[k].attrs.items()
if ak in attrs_to_print
)
if attr_s:
attr_s = " Differing variable attributes:\n" + attr_s
attrs_summary.append(attr_s)

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

# TODO: It should be possible recursively use _diff_mapping_repr
# instead of explicitly handling variable attrs specially.
# That would require some refactoring.
# newdiff = _diff_mapping_repr(
# {k: v for k,v in a_attrs.items() if k in attrs_to_print},
# {k: v for k,v in b_attrs.items() if k in attrs_to_print},
# compat=compat,
# summarizer=summarize_attr,
# title="Variable Attributes"
# )
# temp += [newdiff]

diff_items += [ab_side + s[1:] for ab_side, s in zip(("L", "R"), temp)]

if diff_items:
Expand Down
26 changes: 19 additions & 7 deletions xarray/tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,19 +406,27 @@ def test_diff_dataset_repr(self) -> None:
"var2": ("x", np.array([3, 4], dtype="int64")),
},
coords={
"x": np.array(["a", "b"], dtype="U1"),
"x": (
"x",
np.array(["a", "b"], dtype="U1"),
{"foo": "bar", "same": "same"},
),
"y": np.array([1, 2, 3], dtype="int64"),
},
attrs={"units": "m", "description": "desc"},
attrs={"title": "mytitle", "description": "desc"},
)

ds_b = xr.Dataset(
data_vars={"var1": ("x", np.array([1, 2], dtype="int64"))},
coords={
"x": ("x", np.array(["a", "c"], dtype="U1"), {"source": 0}),
"x": (
"x",
np.array(["a", "c"], dtype="U1"),
{"source": 0, "foo": "baz", "same": "same"},
),
"label": ("x", np.array([1, 2], dtype="int64")),
},
attrs={"units": "kg"},
attrs={"title": "newtitle"},
)

byteorder = "<" if sys.byteorder == "little" else ">"
Expand All @@ -429,8 +437,12 @@ def test_diff_dataset_repr(self) -> None:
(x: 2, y: 3) != (x: 2)
Differing coordinates:
L * x (x) %cU1 'a' 'b'
Differing variable attributes:
foo: bar
R * x (x) %cU1 'a' 'c'
source: 0
Differing variable attributes:
source: 0
foo: baz
Coordinates only on the left object:
* y (y) int64 1 2 3
Coordinates only on the right object:
Expand All @@ -441,8 +453,8 @@ def test_diff_dataset_repr(self) -> None:
Data variables only on the left object:
var2 (x) int64 3 4
Differing attributes:
L units: m
dcherian marked this conversation as resolved.
Show resolved Hide resolved
R units: kg
L title: mytitle
R title: newtitle
Attributes only on the left object:
description: desc"""
% (byteorder, byteorder)
Expand Down
Loading