Skip to content

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

Closed
@wjandrea

Description

@wjandrea

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions