Skip to content

Commit

Permalink
Make sure Interval can be deepcopy-ed, fix python-pendulum#850
Browse files Browse the repository at this point in the history
  • Loading branch information
aploium committed Oct 13, 2024
1 parent df4d44c commit ab5e95b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/pendulum/interval.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import copy
import operator

from datetime import date
Expand Down Expand Up @@ -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,
)
16 changes: 16 additions & 0 deletions tests/interval/test_behavior.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import pickle
import copy

from datetime import timedelta

Expand Down Expand Up @@ -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

0 comments on commit ab5e95b

Please sign in to comment.