Skip to content
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

Merged
merged 3 commits into from
Oct 1, 2024

Conversation

gpshead
Copy link
Member

@gpshead gpshead commented Sep 30, 2024

This comes out of a discovery a colleague made:

Should we create a linter rule for code that looks at timedelta.seconds when they mean timedelta.total_seconds(), and if so, what would that look like? A quick grep suggests we have this bug in a number of places in our code.

which prompted an investigation by @Zac-HD:

of N occurences of .seconds >N-6 of them are this bug on timedeltas, and the rest are attributes of classes we define and can thus change.

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

@gpshead gpshead added docs Documentation in the Doc dir skip issue skip news needs backport to 3.12 bug and security fixes needs backport to 3.13 bugs and security fixes labels Sep 30, 2024
Doc/library/datetime.rst Outdated Show resolved Hide resolved
@gpshead gpshead enabled auto-merge (squash) October 1, 2024 17:51
@terryjreedy
Copy link
Member

LGTM as edited.

@gpshead gpshead merged commit d150e4a into python:main Oct 1, 2024
23 checks passed
@miss-islington-app
Copy link

Thanks @gpshead for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 1, 2024
…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>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 1, 2024
…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>
@bedevere-app
Copy link

bedevere-app bot commented Oct 1, 2024

GH-124862 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Oct 1, 2024
@bedevere-app
Copy link

bedevere-app bot commented Oct 1, 2024

GH-124863 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 bug and security fixes label Oct 1, 2024
gpshead added a commit that referenced this pull request Oct 2, 2024
…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>
@gpshead gpshead deleted the timedelta/totalseconds/warning branch October 2, 2024 04:57
@pganssle
Copy link
Member

pganssle commented Oct 2, 2024

I think this is... fine, but doesn't seem especially specific to seconds? Shouldn't the same also apply to minutes and hours and whatnot?

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, my_td / timedelta(seconds=1)), and then maybe have caution notes on the struct accessors pointing to the central note.

@gpshead
Copy link
Member Author

gpshead commented Oct 3, 2024

I was pondering that as well but timedelta only has .days, .seconds, and .microseconds attributes. Not hours or days.

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 .total_seconds() in this case. But that approach is already covered in the .total_seconds docs for other units.

Yhg1s pushed a commit that referenced this pull request Oct 3, 2024
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip issue skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants