Skip to content
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
19 changes: 19 additions & 0 deletions src/pendulum/duration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from datetime import timedelta
from typing import TYPE_CHECKING
from typing import Any
from typing import cast
from typing import overload

Expand All @@ -16,6 +17,7 @@

if TYPE_CHECKING:
from typing_extensions import Self
from typing_extensions import SupportsIndex


def _divide_and_round(a: float, b: float) -> int:
Expand Down Expand Up @@ -295,6 +297,23 @@ def as_timedelta(self) -> timedelta:
"""
return timedelta(seconds=self.total_seconds())

def __reduce__(
self,
) -> tuple[type[Self], tuple[int, int, int], dict[str, Any]]:
return self.__reduce_ex__(2)

def __reduce_ex__(
self, protocol: SupportsIndex
) -> tuple[type[Self], tuple[int, int, int], dict[str, Any]]:
# The reduction inherited from timedelta only keeps the normalized
# day/second/microsecond constructor arguments, which drops the
# calendar units (years and months) the duration was built with.
# The instance state is preserved alongside them so that pickling
# and copying round-trip without losing information.
_, args = timedelta.__reduce__(self)

return self.__class__, args, dict(self.__dict__)

def __str__(self) -> str:
return self.in_words()

Expand Down
64 changes: 64 additions & 0 deletions tests/duration/test_behavior.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pickle

from copy import copy
from copy import deepcopy
from datetime import timedelta

Expand All @@ -20,6 +21,69 @@ def test_pickle() -> None:
assert it == it2


@pytest.mark.parametrize(
"duration, expected",
[
(
pendulum.duration(years=2, months=3, weeks=1, days=2),
{"years": 2, "months": 3, "weeks": 1, "days": 2},
),
(
pendulum.duration(years=-1, months=-5, weeks=-2, days=-3),
{"years": -1, "months": -5, "weeks": -2, "days": -3},
),
(
pendulum.duration(
years=1,
months=-2,
weeks=1,
days=-4,
hours=5,
minutes=-6,
seconds=7,
microseconds=8,
),
{
"years": 1,
"months": -2,
"weeks": 0,
"days": 3,
"hours": 4,
"minutes": 54,
"seconds": 7,
"microseconds": 8,
},
),
(
pendulum.duration(microseconds=42),
{
"years": 0,
"months": 0,
"weeks": 0,
"days": 0,
"microseconds": 42,
},
),
],
)
def test_pickle_preserves_calendar_units(duration, expected) -> None:
for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
restored = pickle.loads(pickle.dumps(duration, protocol=protocol))

assert restored == duration
assert restored.total_seconds() == duration.total_seconds()
assert str(restored) == str(duration)
assert_duration(restored, **expected)


def test_copy_preserves_calendar_units() -> None:
duration = pendulum.duration(years=2, months=3, weeks=1, days=2)
copied = copy(duration)

assert copied == duration
assert_duration(copied, years=2, months=3, weeks=1, days=2)


def test_comparison_to_timedelta() -> None:
duration = pendulum.duration(days=3)

Expand Down