Skip to content

deepcopy of Duration does not include weeks #870

New issue

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

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

Already on GitHub? # to your account

Open
2 tasks done
gareththackeray opened this issue Jan 3, 2025 · 1 comment
Open
2 tasks done

deepcopy of Duration does not include weeks #870

gareththackeray opened this issue Jan 3, 2025 · 1 comment

Comments

@gareththackeray
Copy link

  • I am on the latest Pendulum version.

  • I have searched the issues of this repo and believe that this is not a dup

  • OS version and name: macOS

  • Pendulum version: 3.0.0

Issue

If a Duration has more than 7 days, deepcopy is wrong as there is no mention of weeks.

def test():
    d = pendulum.Duration(days=9)

    d1 = copy.deepcopy(d)

    assert d1.total_days() == 9

gives

>       assert d1.total_days() == 9
E       assert 2.0 == 9
E        +  where 2.0 = total_days()
E        +    where total_days = Duration(days=2).total_days

This is because there is no weeks=self.weeks in __deepcopy__:

def __deepcopy__(self, _: dict[int, Self]) -> Self:
return self.__class__(
days=self.remaining_days,
seconds=self.remaining_seconds,
microseconds=self.microseconds,
minutes=self.minutes,
hours=self.hours,
years=self.years,
months=self.months,
)

@jgogstad
Copy link

this causes dataclasses.as_dict to break as it depends on copy.deepcopy

quick shell demonstrating the issue

❯ python
Python 3.13.1 (main, Jan  8 2025, 13:21:34) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pendulum
>>> import copy
>>> copy.deepcopy(pendulum.Duration(weeks=1, hours=1))
Duration(hours=1)
>>>

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants