diff --git a/tests/test_core_utils.py b/tests/test_core_utils.py index e9a9c2fd2..f489bf70b 100644 --- a/tests/test_core_utils.py +++ b/tests/test_core_utils.py @@ -53,7 +53,7 @@ def test_unique_grids(): grid2 = grid1d() grid_different = grid1d() - grid_different.attrs["something"] = "different" + grid_different._attrs["something"] = "different" assert len(unique_grids([grid, grid2, grid_different])) == 2 assert len(unique_grids([grid, grid2])) == 1 diff --git a/tests/test_ugrid1d.py b/tests/test_ugrid1d.py index d486d5242..bb597c53e 100644 --- a/tests/test_ugrid1d.py +++ b/tests/test_ugrid1d.py @@ -50,6 +50,13 @@ def test_ugrid1d_init(): assert grid.node_y.flags["C_CONTIGUOUS"] +def test_safe_attrs(): + # .attrs should return a copy + grid = grid1d() + assert grid.attrs == grid.attrs + assert grid._attrs is not grid.attrs + + def test_ugrid1d_alternative_init(): custom_attrs = { "node_dimension": "nNetNode", @@ -466,7 +473,7 @@ def test_equals(): assert grid.equals(grid_copy) xr_grid = grid.to_dataset() assert not grid.equals(xr_grid) - grid_copy.attrs["attr"] = "something_else" + grid_copy._attrs["attr"] = "something_else" # Dataset.identical is called so returns False assert not grid.equals(grid_copy) diff --git a/tests/test_ugrid2d.py b/tests/test_ugrid2d.py index be726d4b1..07a6e0d61 100644 --- a/tests/test_ugrid2d.py +++ b/tests/test_ugrid2d.py @@ -107,6 +107,13 @@ def test_ugrid2d_init(): assert grid._face_edge_connectivity is None +def test_safe_attrs(): + # .attrs should return a copy + grid = grid2d() + assert grid.attrs == grid.attrs + assert grid._attrs is not grid.attrs + + def test_ugrid2d_alternative_init(): custom_attrs = { "node_dimension": "nNetNode", @@ -1448,7 +1455,7 @@ def test_equals(): assert grid.equals(grid_copy) xr_grid = grid.to_dataset() assert not grid.equals(xr_grid) - grid_copy.attrs["attr"] = "something_else" + grid_copy._attrs["attr"] = "something_else" assert not grid.equals(grid_copy) diff --git a/xugrid/ugrid/ugridbase.py b/xugrid/ugrid/ugridbase.py index 9e5830eba..ed77b2646 100644 --- a/xugrid/ugrid/ugridbase.py +++ b/xugrid/ugrid/ugridbase.py @@ -322,7 +322,7 @@ def copy(self): @property def attrs(self): - return self._attrs + return copy.deepcopy(self._attrs) @property def node_dimension(self):