Skip to content

Add __replace__ protocol support to timedelta #135195

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

Open
mpkocher opened this issue Jun 5, 2025 · 3 comments
Open

Add __replace__ protocol support to timedelta #135195

mpkocher opened this issue Jun 5, 2025 · 3 comments
Labels
type-feature A feature request or enhancement

Comments

@mpkocher
Copy link
Contributor

mpkocher commented Jun 5, 2025

Feature or enhancement

Proposal:

Requirements:

  • Add __replace__ support for timedelta
  • Add .replace() to timedelta to be consistent with date and datetime

Currently, both date and datetime have a .replace() method and supports the __replace__ protocol

For unclear reasons, timedelta doesn't follow suit.

In [1]: from datetime import datetime, date, timedelta

In [2]: d = datetime.now()

In [3]: d.replace(day=1)
Out[3]: datetime.datetime(2025, 6, 1, 16, 50, 54, 282957)

In [4]: import copy

In [6]: copy.replace(d, second=1)
Out[6]: datetime.datetime(2025, 6, 5, 16, 50, 1, 282957)

In [7]: d = date.today()

In [8]: d.replace(day=1)
Out[8]: datetime.date(2025, 6, 1)

In [10]: copy.replace(d, day=1)
Out[10]: datetime.date(2025, 6, 1)

In [11]: t = timedelta(seconds=100)

In [12]: copy.replace(t, second=1)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[12], line 1
----> 1 copy.replace(t, second=1)

File ~/miniconda3/envs/core-313/lib/python3.13/copy.py:305, in replace(obj, **changes)
    303 func = getattr(cls, '__replace__', None)
    304 if func is None:
--> 305     raise TypeError(f"replace() does not support {cls.__name__} objects")
    306 return func(obj, **changes)

TypeError: replace() does not support timedelta objects
# Add a code block here, if required

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

@mpkocher mpkocher added the type-feature A feature request or enhancement label Jun 5, 2025
@vstinner
Copy link
Member

vstinner commented Jun 6, 2025

It makes sense to add a replace() method and support the __replace__() protocol for datetime.timedelta.

@StanFromIreland
Copy link
Contributor

I can implement, unless you want to @mpkocher ? And, @pganssle , any reasons to not implement?

@serhiy-storchaka
Copy link
Member

It was not implemented because ambiguous semantic.

Let you have td = timedelta(minutes=5, seconds=102).

>>> from datetime import *
>>> td = timedelta(minutes=5, seconds=102)
>>> td
datetime.timedelta(seconds=402)

What should td.replace(seconds=250) return -- timedelta(minutes=5, seconds=250) or timedelta(seconds=250)? What should td.replace(minutes=3) return -- timedelta(minutes=3, seconds=102), timedelta(minutes=3, seconds=42) or timedelta(seconds=180)? If the last option is the answer in both cases, how does it differ from just using the timedelta constructor? And I haven't even touched on the issue of days and microseconds, which complicates things even more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
Development

No branches or pull requests

4 participants