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

Fix handling of the fold attribute when deep copying DateTime objects #776

Merged
merged 1 commit into from
Dec 15, 2023
Merged
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
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
Loading