Skip to content

Commit

Permalink
Fix handling of the fold attribute when deep copying DateTime objects
Browse files Browse the repository at this point in the history
Fixes #767
  • Loading branch information
sdispater committed Dec 15, 2023
1 parent 1ff9a1c commit 4fa9b8e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/pendulum/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1357,17 +1357,32 @@ def _getstate(
def __reduce__(
self,
) -> tuple[
type[Self], tuple[int, int, int, int, int, int, int, datetime.tzinfo | None]
type[Self],
tuple[int, int, int, int, int, int, int, datetime.tzinfo | None],
]:
return self.__reduce_ex__(2)

def __reduce_ex__(
self, protocol: SupportsIndex
) -> tuple[
type[Self], tuple[int, int, int, int, int, int, int, datetime.tzinfo | None]
type[Self],
tuple[int, int, int, int, int, int, int, datetime.tzinfo | None],
]:
return self.__class__, self._getstate(protocol)

def __deepcopy__(self, _: dict[int, Self]) -> Self:
return self.__class__(
self.year,
self.month,
self.day,
self.hour,
self.minute,
self.second,
self.microsecond,
tzinfo=self.tz,
fold=self.fold,
)

def _cmp(self, other: datetime.datetime, **kwargs: Any) -> int:
# Fix for pypy which compares using this method
# which would lead to infinite recursion if we didn't override
Expand Down
8 changes: 8 additions & 0 deletions tests/datetime/test_behavior.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ def test_deepcopy():
assert dt == deepcopy(dt)


def test_deepcopy_on_transition():
dt = pendulum.datetime(2023, 11, 5, 1, 0, 0, tz="US/Pacific")
clone = deepcopy(dt)

assert dt == clone
assert dt.offset == clone.offset


def test_pickle_timezone():
dt1 = pendulum.timezone("Europe/Amsterdam")
s = pickle.dumps(dt1)
Expand Down

0 comments on commit 4fa9b8e

Please sign in to comment.