From 993dfa9f6ca1eb152eab01c1813e4452b82b108e Mon Sep 17 00:00:00 2001 From: Jakob Kaiser Date: Tue, 13 May 2025 08:17:41 +0000 Subject: [PATCH] fix: deepcopy `quantities.Quantity.deepcopy` assumed that new objects copy the data per defualt. This is no longer true since the adaptation to numpy 2 (#235, commit: ed43021). --- quantities/quantity.py | 4 ---- quantities/tests/test_persistence.py | 9 +++++++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/quantities/quantity.py b/quantities/quantity.py index 74a425f..b93c8a5 100644 --- a/quantities/quantity.py +++ b/quantities/quantity.py @@ -804,10 +804,6 @@ def __reduce__(self): (self.__class__, np.ndarray, (0, ), 'b', ), state) - def __deepcopy__(self, memo_dict): - # constructor copies by default - return Quantity(self.magnitude, self.dimensionality) - def _reconstruct_quantity(subtype, baseclass, baseshape, basetype,): """Internal function that builds a new MaskedArray from the diff --git a/quantities/tests/test_persistence.py b/quantities/tests/test_persistence.py index da26924..859f7c7 100644 --- a/quantities/tests/test_persistence.py +++ b/quantities/tests/test_persistence.py @@ -71,6 +71,15 @@ def test_copy_quantity(self): y = copy.copy(x) self.assertQuantityEqual(x, y) + def test_deepcopy_quantity(self): + x = [1, 2, 3] * pq.m + y = copy.deepcopy(x) + self.assertQuantityEqual(x, y) + + y[0] = 100 * pq.m + self.assertQuantityEqual(x, [1, 2, 3] * pq.m) + + def test_copy_uncertainquantity(self): for dtype in [float, object]: x = UncertainQuantity(20, 'm', 0.2).astype(dtype)