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

gh-118542: improve datetime deprecation warnings #118571

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

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

Copy link

cpython-cla-bot bot 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.

Modules/_datetimemodule.c Outdated Show resolved Hide resolved
Modules/_datetimemodule.c Outdated Show resolved Hide resolved
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.

@@ -1809,7 +1809,9 @@ def utcfromtimestamp(cls, t):
warnings.warn("datetime.datetime.utcfromtimestamp() is deprecated and scheduled "
"for removal in a future version. Use timezone-aware "
"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

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.

@@ -985,7 +986,8 @@ Other constructors, all class methods:

.. deprecated:: 3.12

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.

@@ -5199,7 +5199,8 @@ datetime_utcnow(PyObject *cls, PyObject *dummy)
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"datetime.datetime.utcnow() is deprecated and scheduled for removal in a "
"future version. Use timezone-aware objects to represent datetimes "
"in UTC: datetime.datetime.now(datetime.UTC).", 1))
"in UTC: datetime.datetime.now(datetime.UTC). To maintain compatibility"
"datetime.datetime.now(datetime.UTC).replace(tzinfo=None) may be used.", 1))
Copy link
Member

Choose a reason for hiding this comment

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

It looks like this will have two words joinedtogether.

But do the warning messages really need to specify replacement options at all?

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