From 6b2fdab82f22aa3754c80c8322f826f5ab1101a6 Mon Sep 17 00:00:00 2001 From: Paul Hockett Date: Wed, 28 Sep 2022 17:03:31 -0400 Subject: [PATCH] Added test_deepcopy_nested_attrs() (#2835). (#7086) * Added test_deepcopy_nested_attrs() (#2835). * mark xfail Co-authored-by: Michael Niklas --- doc/whats-new.rst | 3 ++- xarray/tests/test_dataarray.py | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 8ccd15160a6..993a1db05f9 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -105,7 +105,8 @@ Documentation Internal Changes ~~~~~~~~~~~~~~~~ - +- Added test for DataArray attrs deepcopy recursion/nested attrs (:issue:`2835`). + By `Paul hockett `_. .. _whats-new.2022.06.0: diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 8692d49bc0d..8bb314c4f3c 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -6458,6 +6458,31 @@ def test_delete_coords() -> None: assert set(a1.coords.keys()) == {"x"} +@pytest.mark.xfail +def test_deepcopy_nested_attrs() -> None: + """Check attrs deep copy, see :issue:`2835`""" + da1 = xr.DataArray([[1, 2], [3, 4]], dims=("x", "y"), coords={"x": [10, 20]}) + da1.attrs["flat"] = "0" + da1.attrs["nested"] = {"level1a": "1", "level1b": "1"} + + da2 = da1.copy(deep=True) + + da2.attrs["new"] = "2" + da2.attrs.update({"new2": "2"}) + da2.attrs["flat"] = "2" + da2.attrs["nested"]["level1a"] = "2" + da2.attrs["nested"].update({"level1b": "2"}) + + # Coarse test + assert not da1.identical(da2) + + # Check attrs levels + assert da1.attrs["flat"] != da2.attrs["flat"] + assert da1.attrs["nested"] != da2.attrs["nested"] + assert "new" not in da1.attrs + assert "new2" not in da1.attrs + + def test_deepcopy_obj_array() -> None: x0 = DataArray(np.array([object()])) x1 = deepcopy(x0)