Skip to content

gh-118542: improve datetime deprecation warnings#118571

Closed
Max-Herbold wants to merge 6 commits intopython:mainfrom
Max-Herbold:main
Closed

gh-118542: improve datetime deprecation warnings#118571
Max-Herbold wants to merge 6 commits intopython:mainfrom
Max-Herbold:main

Conversation

@Max-Herbold
Copy link

@Max-Herbold Max-Herbold commented May 4, 2024

Add suggestion for code authors to have an in-place, future-proof replacement if desired.

Builds on the initial issue raised by also adding compatibility suggestion for datetime.datetime.fromtimestamp(timestamp, datetime.UTC) -> datetime.datetime.fromtimestamp(timestamp). By not providing a timezone, the returned object is not timezone aware.

fixes #118542

@ghost
Copy link

ghost commented May 4, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app
Copy link

bedevere-app bot commented May 4, 2024

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Copy link
Member

@arhadthedev arhadthedev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small typo nitpicks.

Max-Herbold and others added 2 commits May 4, 2024 22:13
Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
@srittau
Copy link
Contributor

srittau commented May 6, 2024

Could you also apply a similar change to the deprecation notice in the datetime module documentation?

Copy link
Member

@pganssle pganssle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think we can make it clearer that switching to timezone-aware datetimes is a breaking change if the datetimes you are using them with are exposed to end users, I am still firmly against the idea of anything that looks like recommending an alternative way to get naive datetimes that represent UTC.

"objects to represent datetimes in UTC: "
"datetime.datetime.fromtimestamp(t, datetime.UTC).",
"datetime.datetime.fromtimestamp(t, datetime.UTC). To maintain "
"compatibility datetime.datetime.fromtimestamp(timestamp) "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is right?

Copy link
Author

@Max-Herbold Max-Herbold May 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was observed, if no timezone is provided, fromtimestamp returns a naïve datetime object.


Use :meth:`datetime.fromtimestamp` with :attr:`UTC` instead.
Use :meth:`datetime.fromtimestamp` with :attr:`UTC` instead. To maintain
compatibility :meth:`datetime.fromtimestamp`without :attr:`UTC` may be used.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do decide to offer this approach, I would be much clearer in the message that doing this is effectively equivalent to suppressing the warning without fixing the underlying issue.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true, this issue was raised (and I had the same experience) because there was no in-place replacement to maintain compatibility provided. I ended up at the same conclusion currently proposed several weeks before the original issue was opened but after some significant time.

Is this the correct understanding here; that the proposed modifications correctly address the case where code written for py<3.12 being run on py>=3.12. But do not adequately address (or propose a misleading solution when) the deprecated functions being called when new code is being written in py>=3.12?

@bedevere-app
Copy link

bedevere-app bot commented May 7, 2024

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@Max-Herbold Max-Herbold closed this Aug 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

utcnow deprecation note is misleading

5 participants

Comments