Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-70647: Better promote how to safely parse yearless dates in datetime.#116179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
gpshead wants to merge7 commits intopython:main
base:main
Choose a base branch
Loading
fromgpshead:docs/datetime/02-29

Conversation

@gpshead
Copy link
Member

@gpsheadgpshead commentedMar 1, 2024
edited by github-actionsbot
Loading

Every four years people encounter this in part because it just isn't obvious that partial incomplete date parsing is not whatdatetime.strptime is designed for. This moves the footnote up to a more explanatory inline note with a code example.

We'd love the default year value for datetime to be different, but changing that could have other consequences for existing code. This documented workaroundalways works.


📚 Documentation preview 📚:https://cpython-previews--116179.org.readthedocs.build/

…datetime.Every four years people encounter this because it just isn't obvious.This moves the footnote up to a note with a code example.We'd love to change the default year value for datetime but doingthat could have other consequences for existing code.  This documentedworkaround *always* works.
@gpsheadgpshead added the docsDocumentation in the Doc dir labelMar 1, 2024
@gpsheadgpshead self-assigned thisMar 1, 2024
@gpsheadgpshead marked this pull request as ready for reviewMarch 1, 2024 08:05
@encukou
Copy link
Member

Perhaps this should be only merged to the backport branches, not main.

@willingc
Copy link
Contributor

@gpshead Looks like this may need some tidying up if you are still interested.

@StanFromIreland
Copy link
Member

This has already been partially implemented (the smaller note) do we still want to add the larger note since its deprecated anyway?

@gpsheadgpshead added needs backport to 3.12only security fixes needs backport to 3.13bugs and security fixes labelsMar 21, 2025
@gpsheadgpshead moved this fromTodo toIn Progress inDocs PRsMar 21, 2025
>>>from datetimeimport datetime
>>>import warnings
>>>value="2/29"
>>>with warnings.catch_warnings():
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

docs folks: Do we have a way to do this ignoring of the DeprecationWarning in the doctest code without polluting the example code with something I don't want anyone to ever write in their code?

doctest seems to turn the warning into an error which makes it show up and require matching and prevents the actual interesting exception from being raised. (see the earlier failing builds on this PR)

Copy link
Member

Choose a reason for hiding this comment

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

Here's a way, I don't know if it's the best way:

..testsetup::# doctest seems to turn the warning into an error which makes it# show up and require matching and prevents the actual interesting# exception from being raised.# Manually apply the `catch_warnings` context managerimportwarningscatch_warnings=warnings.catch_warnings()catch_warnings.__enter__()..testcleanup::catch_warnings.__exit__()..doctest::>>>fromdatetimeimportdatetime>>>importwarnings>>>value="2/29">>>datetime.strptime(value,"%m/%d")Traceback (mostrecentcalllast):    ...ValueError:day29mustbeinrange1..28formonth2inyear1900>>>datetime.strptime(f"1904{value}","%Y %m/%d")datetime.datetime(1904,2,29,0,0)

StanFromIreland reacted with thumbs up emoji

Choose a reason for hiding this comment

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

Maybe the error is something to do with the C implementation. Depreciation errors don’t show up in the repl unless they are from a C extension resulting in them being forcibly displayed.

@gpsheadgpshead removed the needs backport to 3.12only security fixes labelMar 21, 2025
@serhiy-storchakaserhiy-storchaka added the needs backport to 3.14bugs and security fixes labelMay 8, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@encukouencukouencukou left review comments

@StanFromIrelandStanFromIrelandStanFromIreland left review comments

@itamaroitamaroitamaro approved these changes

@pgansslepgansslepganssle approved these changes

@abalkinabalkinAwaiting requested review from abalkinabalkin is a code owner

@willingcwillingcAwaiting requested review from willingc

Assignees

@gpsheadgpshead

Labels

docsDocumentation in the Doc dirneeds backport to 3.13bugs and security fixesneeds backport to 3.14bugs and security fixesskip news

Projects

Status: In Progress

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

7 participants

@gpshead@encukou@willingc@StanFromIreland@itamaro@pganssle@serhiy-storchaka

[8]ページ先頭

©2009-2025 Movatter.jp