python / python/cpython

`utcnow` deprecation note is misleading

Open
#118,542 5 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3.12 3.13 docs
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Documentation

Currently, the deprecation notice for datetime.utcnow() reads:

Deprecated since version 3.12: Use datetime.now() with UTC instead.

Unfortunately, applying this suggestion verbatim can easily break user code – especially when applying it to libraries. (See for example https://foss.heptapod.net/openpyxl/openpyxl/-/issues/2051). These datetime-related TypeErrors are easy to introduce, and fairly hard to fix. I suggest to amend the deprecation notice:

Deprecated since version 3.12: Use datetime.now(UTC) instead. Please note that this will return an aware datetime object. If you need to remain compatible, use datetime.now(UTC).replace(tzinfo=None).

Maybe someone can come up with a better wording.

Linked PRs
  • gh-118571

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review the datetime.datetime.utcnow() documentation at the linked datetime page and compare the proposed wording with PR gh-118571. Done means the deprecation note clearly warns that datetime.now(UTC) returns an aware datetime and states the replace(tzinfo=None) compatibility option, or adopts an agreed better wording.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.