diff --git a/src/pendulum/interval.py b/src/pendulum/interval.py index 19c91a6a..ef9f27ff 100644 --- a/src/pendulum/interval.py +++ b/src/pendulum/interval.py @@ -1,5 +1,6 @@ from __future__ import annotations +import copy import operator from datetime import date @@ -453,3 +454,10 @@ def __eq__(self, other: object) -> bool: def __ne__(self, other: object) -> bool: return not self.__eq__(other) + + def __deepcopy__(self, memodict: dict[int, Self]) -> Self: + return self.__class__( + copy.deepcopy(self.start), + copy.deepcopy(self.end), + self._absolute, + ) diff --git a/tests/interval/test_behavior.py b/tests/interval/test_behavior.py index 96a1f426..cb165972 100644 --- a/tests/interval/test_behavior.py +++ b/tests/interval/test_behavior.py @@ -1,6 +1,7 @@ from __future__ import annotations import pickle +import copy from datetime import timedelta @@ -65,3 +66,18 @@ def test_inequality(): assert interval1 != interval2 assert interval1 != interval3 + + +def test_deepcopy(): + dt1 = pendulum.datetime(2016, 11, 18) + dt2 = pendulum.datetime(2016, 11, 20) + + interval = dt2 - dt1 + + interval2 = copy.deepcopy(interval) + + assert interval == interval2 + # make sure it's a deep copy + assert interval is not interval2 + assert interval.start is not interval2.start + assert interval.end is not interval2.end