]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] Highlight `datetime.timedelta.seconds` vs `.total_seconds()` in docs. (GH...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 2 Oct 2024 04:57:07 +0000 (06:57 +0200)
committerGitHub <noreply@github.com>
Wed, 2 Oct 2024 04:57:07 +0000 (21:57 -0700)
Highlight `datetime.timedelta.seconds` vs `.total_seconds()` in docs. (GH-124811)

Thanks to the reviewers for suggesting the use of a "caution" section instead of "warning" or "note".
(cherry picked from commit d150e4abcfc13770c2d239878ed337fb53e51de5)

Co-authored-by: Gregory P. Smith <greg@krypto.org>
Doc/library/datetime.rst

index e3ddd8ca82edb676f0898255dcdda2c560f1f3f6..8f5ea47da0db252b9960b3502a06dcae663eff9a 100644 (file)
@@ -295,6 +295,20 @@ Instance attributes (read-only):
 
    Between 0 and 86,399 inclusive.
 
+   .. caution::
+
+      It is a somewhat common bug for code to unintentionally use this attribute
+      when it is actually intended to get a :meth:`~timedelta.total_seconds`
+      value instead:
+
+      .. doctest::
+
+         >>> from datetime import timedelta
+         >>> duration = timedelta(seconds=11235813)
+         >>> duration.days, duration.seconds
+         (130, 3813)
+         >>> duration.total_seconds()
+         11235813.0
 
 .. attribute:: timedelta.microseconds
 
@@ -351,7 +365,7 @@ Supported operations:
 |                                | same value. (2)                               |
 +--------------------------------+-----------------------------------------------+
 | ``-t1``                        | Equivalent to ``timedelta(-t1.days,           |
-|                                | -t1.seconds*, -t1.microseconds)``,            |
+|                                | -t1.seconds, -t1.microseconds)``,             |
 |                                | and to ``t1 * -1``. (1)(4)                    |
 +--------------------------------+-----------------------------------------------+
 | ``abs(t)``                     | Equivalent to ``+t`` when ``t.days >= 0``,    |