Skip to content

Docs: import datetime as dt in examples - #145315

Merged
hugovk merged 3 commits into
python:mainfrom
hugovk:docs-import-datetime-as-dt
Mar 6, 2026
Merged

Docs: import datetime as dt in examples#145315
hugovk merged 3 commits into
python:mainfrom
hugovk:docs-import-datetime-as-dt

Conversation

@hugovk

@hugovkhugovk commented Feb 27, 2026

Copy link
Copy Markdown
Member

See https://discuss.python.org/t/renaming-datetime-datetime-to-datetime-datetime/26279/4 and the following post for support.

There's a few other examples in other files we can update after this.


📚 Documentation preview 📚: https://cpython-previews--145315.org.readthedocs.build/en/145315/library/datetime.html

@hugovk
hugovkforce-pushed the docs-import-datetime-as-dt branch from 068dc30 to 904011aCompareFebruary 27, 2026 13:49
@hugovk
hugovk marked this pull request as ready for review February 27, 2026 13:58

@StanFromIrelandStanFromIreland left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is churn in my opinion, and quite a lot of it. I agree with Paul Moore's comment, we need not enforce such things. I also don't see consensus in the thread for this, from a rough count I gather more people are against the change.

Comment threadDoc/includes/tzinfo_examples.py Outdated
@hugovk

Copy link
Copy Markdown
MemberAuthor

I agree with Paul Moore's comment, we need not enforce such things.

There's no enforcement here, this is a docs-only change. You can still import datetime if you prefer. There are no stdlib (or linter) changes.

I also don't see consensus in the thread for this, from a rough count I gather more people are against the change.

I disagree. I see more people against code and linter changes, but maybe just a couple against docs changes. I see 12 supporting Terry's docs suggestion (plus him), and another couple also supporting via comments.

@nedbat

Copy link
Copy Markdown
Member

I'm in favor of changing these examples in the docs. It provides a clear distinction between the module and the class, while gently showing importing the module as a preference over importing the classes directly.

Comment threadDoc/includes/tzinfo_examples.py Outdated
@treyhunner

Copy link
Copy Markdown
Member

I do struggle with new Python learners hitting confusion about the different between the module and the class datetime, especially in example code like datetime.now().

I tend to use the from datetime import ... convention in my own code, but that's the convention that's most likely to cause datetime confusion for new learners.

I am in favor of changing the documentation so that they do not use the from datetime import ... syntax. I think this import datetime as dt convention is a good way to do that.

I read through all the changes and they look good to me. 👍

@picnixz

Copy link
Copy Markdown
Member

I'm also in favor of this change. Too many times my IDE bite me by importing the class instead of the module or vice-versa so import datetime as dt would definitely help newcomers to distinguish between the module, the class, and the instace method datetime.datetime.time!

@StanFromIrelandStanFromIreland left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the docs experts think it's best, I won't block.

@hugovk
hugovk merged commit d3b6faf into python:mainMar 6, 2026
32 checks passed
@hugovk
hugovk deleted the docs-import-datetime-as-dt branch March 6, 2026 11:18
@miss-islington-app

Copy link
Copy Markdown

Thanks @hugovk for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Mar 6, 2026
(cherry picked from commit d3b6faf)
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
@miss-islington-app

Copy link
Copy Markdown

Sorry, @hugovk, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker d3b6faf9758dce236d45c708a450437cdc3e97cd 3.13

@bedevere-app

Copy link
Copy Markdown

GH-145583 is a backport of this pull request to the 3.14 branch.

@bedevere-appbedevere-appBot removed the needs backport to 3.14 bugs and security fixes label Mar 6, 2026
hugovk added a commit to hugovk/cpython that referenced this pull request Mar 6, 2026
(cherry picked from commit d3b6faf)
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
@bedevere-app

Copy link
Copy Markdown

GH-145584 is a backport of this pull request to the 3.13 branch.

@bedevere-appbedevere-appBot removed the needs backport to 3.13 bugs and security fixes label Mar 6, 2026
hugovk added a commit that referenced this pull request Mar 6, 2026
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
@hugovk

Copy link
Copy Markdown
MemberAuthor

There's a few other examples in other files we can update after this.

Please see PR #145640.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@hugovk@nedbat@treyhunner@picnixz@StanFromIreland