-
-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
Highlight datetime.timedelta.seconds
vs .total_seconds()
in docs.
#124811
Conversation
LGTM as edited. |
Thanks @gpshead for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13. |
…pythonGH-124811) Thanks to the reviewers for suggesting the use of a "caution" section instead of "warning" or "note". (cherry picked from commit d150e4a) Co-authored-by: Gregory P. Smith <greg@krypto.org>
…pythonGH-124811) Thanks to the reviewers for suggesting the use of a "caution" section instead of "warning" or "note". (cherry picked from commit d150e4a) Co-authored-by: Gregory P. Smith <greg@krypto.org>
GH-124862 is a backport of this pull request to the 3.13 branch. |
GH-124863 is a backport of this pull request to the 3.12 branch. |
…n docs. (GH-124811) (GH-124863) 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 d150e4a) Co-authored-by: Gregory P. Smith <greg@krypto.org>
I think this is... fine, but doesn't seem especially specific to I think it might be a good idea to refactor this into a single note about how to get total durations (usually dividing by the unit you want, |
I was pondering that as well but So this caution sandwiched inbetween the others at least hopefully provides the right hint for those as well. But those two being the less commonly used bookend most and least significant terms are much more obvious when they don't give what someone wants in most cases as microseconds would quickly overflow and days would always lack a fraction so I feel like they're less likely to actually be used in a significantly buggy manner. The value of the cautionary note is in having it right next to the docs for the thing people otherwise naturally reach for. So if it is moved to be reworded as general covering all three, please keep it in that section right next to the attributes overall. The division by a dynamically created temporary value of units is less intuitive (and probably slower) than just calling |
…n docs. (GH-124811) (#124862) 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 d150e4a) Co-authored-by: Gregory P. Smith <greg@krypto.org>
This comes out of a discovery a colleague made:
which prompted an investigation by @Zac-HD:
meaning basically 100% of uses of
datetime.timedelta.seconds
should've been using.total_seconds()
...If you know that you will never be encountering a timedelta longer than 1 day there is no difference between the two (which is why code often gets away with this...), but how often does code guarantee that and would fail in a surprising manner when >=1 day deltas are encountered as a result of the mistake?
Documenting the caveat seems useful to guide people away from this mistake.
Linter ecosystem checks to highlight this deemed useful but are outside of our purview.
📚 Documentation preview 📚: https://cpython-previews--124811.org.readthedocs.build/
https://cpython-previews--124811.org.readthedocs.build/en/124811/library/datetime.html#datetime.timedelta.seconds