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

Let grid attrs property return a deepcopy #273

Merged
merged 1 commit into from
Aug 8, 2024
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
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
Loading