Skip to content

Docs: import datetime as dt in examples#145315

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

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

Conversation

@hugovk
Copy link
Member

@hugovk hugovk commented Feb 27, 2026

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 hugovk added skip issue skip news needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Feb 27, 2026
@hugovk hugovk force-pushed the docs-import-datetime-as-dt branch from 068dc30 to 904011a Compare February 27, 2026 13:49
@hugovk hugovk marked this pull request as ready for review February 27, 2026 13:58
Copy link
Member

@StanFromIreland StanFromIreland left a comment

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.

@hugovk
Copy link
Member Author

hugovk commented Mar 1, 2026

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
Member

nedbat commented Mar 3, 2026

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.

@treyhunner
Copy link
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
Member

picnixz commented Mar 5, 2026

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!

Copy link
Member

@StanFromIreland StanFromIreland left a comment

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:main Mar 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

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

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

bedevere-app bot commented Mar 6, 2026

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

@bedevere-app bedevere-app bot 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

bedevere-app bot commented Mar 6, 2026

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

@bedevere-app bedevere-app bot 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>
Sign up for free to 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