Closed
Description
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()
anddatetime.datetime.utcfromtimestamp()
, and notdatetime.utcnow()
anddatetime.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
- gh-106392: Fixed datetime UTC deprecation warnings use the name "datetime" inconsistently #106392 (#106392) #106405
- gh-106392: Fix inconsistency in deprecation warnings #106436
- [3.12] gh-106392: Fix inconsistency in deprecation warnings (GH-106436) #108792
- gh-106392: Fix inconsistency in deprecation warnings in datetime module #114761
- gh-106392: Fix consistency in pydatetime error messages #114764
- [3.12] gh-106392: Fix inconsistency in deprecation warnings in datetime module (GH-114761) #114767