Skip to content
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
8 changes: 8 additions & 0 deletions decent_array/_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,14 @@ def __str__(self) -> str:
"""Stringify the wrapped value, not the wrapper."""
return str(self.value)

# Copy -----------------------------------------------------------------

def __deepcopy__(self, memo: dict[int, Any]) -> Array:
"""Deep copy of the array."""
copied = self._backend.copy(self)
memo[id(self)] = copied
return copied

# Properties -----------------------------------------------------------

@property
Expand Down
14 changes: 14 additions & 0 deletions tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,3 +567,17 @@ def test_item_n_dim(backend: tuple) -> None:
a = _create_array([1.0, 2.0])
with pytest.raises(TypeError, match=r"Only 0-dim arrays"):
_ = a.item()


# Copy -------------------------------------------------------------------


def test_deepcopy(backend: tuple) -> None:
from copy import deepcopy

src = iop.from_numpy(np.array([1.0, 2.0, 3.0], dtype=np.float32))
dst = deepcopy(src)
np.testing.assert_allclose(_np(dst), [1.0, 2.0, 3.0])
# Mutating the copy shouldn't affect the original.
dst[0] = 99.0
np.testing.assert_allclose(_np(src), [1.0, 2.0, 3.0])