Skip to content

Commit

Permalink
Merge pull request #273 from Deltares/safe-attrs
Browse files Browse the repository at this point in the history
Let grid attrs property return a deepcopy
  • Loading branch information
Huite authored Aug 8, 2024
2 parents e5487e4 + 7da2aa0 commit 72fbc91
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion tests/test_core_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion tests/test_ugrid1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)

Expand Down
9 changes: 8 additions & 1 deletion tests/test_ugrid2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)


Expand Down
2 changes: 1 addition & 1 deletion xugrid/ugrid/ugridbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def copy(self):

@property
def attrs(self):
return self._attrs
return copy.deepcopy(self._attrs)

@property
def node_dimension(self):
Expand Down

0 comments on commit 72fbc91

Please sign in to comment.