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

datetime UTC deprecation warnings use the name "datetime" inconsistently #106392

Closed
wjandrea opened this issue Jul 4, 2023 · 4 comments
Closed
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@wjandrea
Copy link
Contributor

wjandrea commented Jul 4, 2023

Bug report

The UTC deprecation warnings (added in #103858) use the name datetime to refer to both the object and the module, which is confusing. Like, if I copy-paste the code from the warnings, it doesn't work:

>>> import datetime
>>> datetime.datetime.utcnow()
<stdin>:1: DeprecationWarning: datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.now(datetime.UTC).
datetime.datetime(2023, 7, 3, 23, 35, 25, 850070)
>>> datetime.now(datetime.UTC)  # <-- Copy-pasted but doesn't work
  ...
AttributeError: module 'datetime' has no attribute 'now'
>>> datetime.datetime.now(datetime.UTC)  # Fixed
datetime.datetime(2023, 7, 3, 23, 35, 32, 898186, tzinfo=datetime.timezone.utc)
>>> 
>>> timestamp = 0
>>> datetime.datetime.utcfromtimestamp(timestamp)
<stdin>:1: DeprecationWarning: datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.fromtimestamp(timestamp, datetime.UTC).
datetime.datetime(1970, 1, 1, 0, 0)
>>> datetime.fromtimestamp(timestamp, datetime.UTC)  # <-- Copy-pasted but doesn't work
  ...
AttributeError: module 'datetime' has no attribute 'fromtimestamp'
>>> datetime.datetime.fromtimestamp(timestamp, datetime.UTC)  # Fixed
datetime.datetime(1970, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)

To fix it, I'd just put datetime.datetime where necessary. LMK if you want me to submit a PR.

By the way

#104542 brought up the same thing about the deprecated names, but I think it's fine personally:

It's really datetime.datetime.utcnow() and datetime.datetime.utcfromtimestamp(), and not datetime.utcnow() and datetime.utcfromtimestamp().

#105544 might also be relevant, I'm not sure.

Your environment

  • CPython versions tested on: 3.12.0b3
  • Operating system and architecture: Ubuntu 20.04 x64

Linked PRs

@wjandrea wjandrea added the type-bug An unexpected behavior, bug, or error label Jul 4, 2023
@terryjreedy
Copy link
Member

The specific inconsistency is that datetime.now(datetime.UTC) uses datetime with two different meanings, first class then module. So the expression can never be correct regardless of the import. I agree with expanding the first datetime to what works with import datetime. This is what @hugovk did with the What'sNew entry. Users should then be able to figure out that the alternative is datetime.now(UTC), with the module label stripped off in both places after both objects are imported from the module.

@hugovk
Copy link
Member

hugovk commented Jul 4, 2023

@wjandrea Thanks for the report, would you like to create a PR to update the deprecation warning text?

yctomwang added a commit to yctomwang/cpython that referenced this issue Jul 4, 2023
… "datetime" inconsistently python#106392 (python#106392)

Replace Modules/_datetimemodule.c's PyExc_DeprecationWarning with 'in UTC: datetime.now(timezone.utc)'.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 1, 2023
…106436)

They used "datetime" to refer to both the object and the module.
(cherry picked from commit d5c5d4b)

Co-authored-by: William Andrea <[email protected]>
hauntsaninja pushed a commit that referenced this issue Sep 1, 2023
They used "datetime" to refer to both the object and the module.
Yhg1s pushed a commit that referenced this issue Sep 2, 2023
… (#108792)

gh-106392: Fix inconsistency in deprecation warnings (GH-106436)

They used "datetime" to refer to both the object and the module.
(cherry picked from commit d5c5d4b)

Co-authored-by: William Andrea <[email protected]>
@iritkatriel iritkatriel added the stdlib Python modules in the Lib dir label Nov 26, 2023
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Jan 30, 2024
@serhiy-storchaka
Copy link
Member

The C implementation was fixed in GH-108073. GH-106436 fixed the Python implementation incompletely, it left inconsistent references to methods at the beginning of warning messages.

@hauntsaninja
Copy link
Contributor

hauntsaninja commented Jan 30, 2024

Ah sorry, I was mainly looking at the consistency between datetime.fromtimestamp and datetime.UTC needed to make that expression work, opened #114764

hauntsaninja added a commit to hauntsaninja/cpython that referenced this issue Jan 30, 2024
Previously python#106436 fixed consistency within the suggested replacement,
but not in the entire message
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jan 30, 2024
…e module (pythonGH-114761)

(cherry picked from commit dc4cd2c)

Co-authored-by: Serhiy Storchaka <[email protected]>
hauntsaninja pushed a commit that referenced this issue Jan 31, 2024
…me module (GH-114761) (#114767)

gh-106392: Fix inconsistency in deprecation warnings in datetime module (GH-114761)
(cherry picked from commit dc4cd2c)

Co-authored-by: Serhiy Storchaka <[email protected]>
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
Neecrownsmith added a commit to Neecrownsmith/PyTweetToolkit that referenced this issue Nov 22, 2024
Solved utcfromtimestamp deprecation by using timezone-aware objects to represent datetimes.

(datetime.datetime.fromtimestamp(timestamp, datetime.UTC)  

Reference: python/cpython#106392
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
6 participants