Skip to content

Commit

Permalink
Added test_deepcopy_nested_attrs() (pydata#2835).
Browse files Browse the repository at this point in the history
  • Loading branch information
phockett committed Sep 26, 2022
1 parent 212a5d7 commit f34eea1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ Documentation

Internal Changes
~~~~~~~~~~~~~~~~

- Added test for DataArray attrs deepcopy recursion/nested attrs (:issue:`2835`).
By `Paul hockett <https://github.com/phockett>`_.

.. _whats-new.2022.06.0:

Expand Down
29 changes: 29 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -6414,6 +6414,35 @@ def test_delete_coords() -> None:
assert set(a1.coords.keys()) == {"x"}


def test_deepcopy_nested_attrs() -> None:
"""Check attrs deep copy, see :issue:`2835`"""
da1 = xr.DataArray(np.random.randn(2, 3), dims=("x", "y"), coords={"x": [10, 20]})
da1.attrs["flat"] = "0"
da1.attrs["nested"] = {"level1": "1"}

da2 = da1.copy(deep=True)

da2.attrs["flat"] = "2" # Test base level
# data2.attrs['nested']['level1'] = '2' # Fails - overwrites data
da2.attrs["nested"].update({"level1": "2"}) # Fails in 2022.3.0 - overwrites da1.

# Coarse test
assert not da1.identical(da2)

# Check attrs levels
assert da1.attrs["flat"] != da2.attrs["flat"]
assert da1.attrs["nested"] != da2.attrs["nested"]

# # Explicit deepcopy of attrs - this should alway work and can be used as an additional test case
# da3 = da1.copy(deep=True)
# da3.attrs = deepcopy(da1.attrs)
# da3.attrs['flat']='3' # OK
# da3.attrs['nested'].update({'level1':'3'})
# assert not da1.identical(da3)
# assert da1.attrs['flat'] != da3.attrs['flat']
# assert da1.attrs['nested'] != da3.attrs['nested']


def test_deepcopy_obj_array() -> None:
x0 = DataArray(np.array([object()]))
x1 = deepcopy(x0)
Expand Down

0 comments on commit f34eea1

Please sign in to comment.