Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure Interval can be deepcopy-ed, fix #850 #851

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 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
Loading