From 73e3654c14e072adb1bcaaded501b2d306081990 Mon Sep 17 00:00:00 2001 From: nicola-bastianello Date: Fri, 4 Sep 2026 15:32:36 +0200 Subject: [PATCH] feat: add __deepcopy__ to Array --- decent_array/_array.py | 8 ++++++++ tests/test_array.py | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/decent_array/_array.py b/decent_array/_array.py index 5580c4a..81f4f1d 100644 --- a/decent_array/_array.py +++ b/decent_array/_array.py @@ -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 diff --git a/tests/test_array.py b/tests/test_array.py index 9ca8d3d..0e7faf9 100644 --- a/tests/test_array.py +++ b/tests/test_array.py @@ -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])