8000 [3.13] gh-115684: Clarify datetime `replace` documentation (GH-116519) by miss-islington · Pull Request #131676 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.13] gh-115684: Clarify datetime replace documentation (GH-116519) #131676

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

Merged
merged 1 commit into from
Apr 8, 2025
Merged
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
gh-115684: Clarify datetime replace documentation (GH-116519)
* Clarify datetime `replace` documentation

In GH-115684, HopedForLuck noted that `datetime.date.replace()`
documentation was confusing because it looked like it would be changing
immutable objects.

This documentation change specifies that the `replace()` methods in
`datetime` return new objects. This uses similar wording to the
documentation for `datetime.combine()`, which specifies that a new
datetime is returned. This is also similar to wording for
`string.replace()`, except `string.replace()` emphasizes that a "copy"
is returned.

Resolves GH-115684.

* Include reviewer comments

Thanks Privat33r-dev for the comments!

---------
(cherry picked from commit d2d8862)

Co-authored-by: David Lowry-Duda <david@lowryduda.com>
Co-authored-by: Paul Ganssle <1377457+pganssle@users.noreply.github.com>
  • Loading branch information
2 people authored and miss-islington committed Mar 24, 2025
commit 00376afae070902a10fb73e029a57b250677e97b
24 changes: 12 additions & 12 deletions Doc/library/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -666,8 +666,8 @@ Instance methods:

.. method:: date.replace(year=self.year, month=self.month, day=self.day)

Return a date with the same value, except for those parameters given new
values by whichever keyword arguments are specified.
Return a new :class:`date` object with the same values, but with specified
parameters updated.

Example::

Expand All @@ -676,8 +676,8 @@ Instance methods:
>>> d.replace(day=26)
datetime.date(2002, 12, 26)

:class:`date` objects are also supported by generic function
:func:`copy.replace`.
The generic function :func:`copy.replace` also supports :class:`date`
objects.


.. method:: date.timetuple()
Expand Down Expand Up @@ -1315,10 +1315,10 @@ Instance methods:
hour=self.hour, minute=self.minute, second=self.second, microsecond=self.microsecond, \
tzinfo=self.tzinfo, *, fold=0)

Return a datetime with the same attributes, except for those attributes given
new values by whichever keyword arguments are specified. Note that
``tzinfo=None`` can be specified to create a naive datetime from an aware
datetime with no conversion of date and time data.
Return a new :class:`datetime` object with the same attributes, but with
specified parameters updated. Note that ``tzinfo=None`` can be specified to
create a naive datetime f 8648 rom an aware datetime with no conversion of date
and time data.

:class:`.datetime` objects are also supported by generic function
:func:`copy.replace`.
Expand Down Expand Up @@ -1893,10 +1893,10 @@ Instance methods:
.. method:: time.replace(hour=self.hour, minute=self.minute, second=self.second, \
microsecond=self.microsecond, tzinfo=self.tzinfo, *, fold=0)

Return a :class:`.time` with the same value, except for those attributes given
new values by whichever keyword arguments are specified. Note that
``tzinfo=None`` can be specified to create a naive :class:`.time` from an
aware :class:`.time`, without conversion of the time data.
Return a new :class:`.time` with the same values, but with specified
parameters updated. Note that ``tzinfo=None`` can be specified to create a
naive :class:`.time` from an aware :class:`.time`, without conversion of the
time data.

:class:`.time` objects are also supported by generic function
:func:`copy.replace`.
Expand Down
Loading
0