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 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions Doc/library/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,8 @@ Other constructors, all class methods:

.. deprecated:: 3.12

Use :meth:`datetime.now` with :attr:`UTC` instead.
Use :meth:`datetime.now` with :attr:`UTC` instead. To maintain compatibility
:meth:`datetime.datetime.now(datetime.UTC).replace(tzinfo=None)` may be used.


.. classmethod:: datetime.fromtimestamp(timestamp, tz=None)
Expand Down Expand Up @@ -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?



.. classmethod:: datetime.fromordinal(ordinal)
Expand Down
9 changes: 7 additions & 2 deletions Lib/_pydatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

"may be used.",
DeprecationWarning,
stacklevel=2)
return cls._fromtimestamp(t, True, None)
Expand All @@ -1827,7 +1829,10 @@ def utcnow(cls):
warnings.warn("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).",
"datetime.datetime.now(datetime.UTC). To maintain "
"compatibility "
"datetime.datetime.now(datetime.UTC).replace(tzinfo=None) "
"may be used.",
DeprecationWarning,
stacklevel=2)
t = _time.time()
Expand Down