Skip to content

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

Merged
gpshead merged 10 commits into
python:mainfrom
gpshead:docs/datetime/02-29
Dec 21, 2025
Merged

gh-70647: Better promote how to safely parse yearless dates in datetime.#116179
gpshead merged 10 commits into
python:mainfrom
gpshead:docs/datetime/02-29

Conversation

@gpshead

@gpsheadgpshead commented Mar 1, 2024

Copy link
Copy Markdown
Member

Every four years people encounter this in part because it just isn't obvious that partial incomplete date parsing is not what datetime.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 workaround always 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 doing
that could have other consequences for existing code. This documented
workaround *always* works.
@gpsheadgpshead added the docs Documentation in the Doc dir label Mar 1, 2024
@gpsheadgpshead self-assigned this Mar 1, 2024
@gpshead
gpshead marked this pull request as ready for review March 1, 2024 08:05
Comment threadDoc/library/datetime.rst Outdated
@encukou

Copy link
Copy Markdown
Member

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

@willingc

Copy link
Copy Markdown
Contributor

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

@StanFromIreland

Copy link
Copy Markdown
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.12 only security fixes needs backport to 3.13 bugs and security fixes labels Mar 21, 2025
@gpsheadgpshead moved this from Todo to In Progress in Docs PRsMar 21, 2025
Comment threadDoc/library/datetime.rst Outdated
@gpsheadgpshead removed the needs backport to 3.12 only security fixes label Mar 21, 2025
@serhiy-storchakaserhiy-storchaka added the needs backport to 3.14 bugs and security fixes label May 8, 2025
@gpsheadgpshead added needs backport to 3.13 bugs and security fixes and removed needs backport to 3.13 bugs and security fixes labels Dec 21, 2025
@gpshead
gpshead merged commit b8d3fdd into python:mainDec 21, 2025
34 checks passed
@github-project-automationgithub-project-automationBot moved this from In Progress to Done in Docs PRsDec 21, 2025
@miss-islington-app

Copy link
Copy Markdown

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

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Dec 21, 2025
…datetime. (pythonGH-116179)
* pythongh-70647: Better promote how to safely parse yearless dates in 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 doing
that could have other consequences for existing code. This documented
workaround *always* works.
* doctest code within note is bad, dedent.
* Update to match the error message.
* remove no longer referenced footnote
* ignore the warning in the doctest
* use Petr's suggestion for the docs to hide the warning processing
* cover date.strptime (3.14) as well
(cherry picked from commit b8d3fdd)
Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com>
@bedevere-app

Copy link
Copy Markdown

GH-143037 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 Dec 21, 2025
@gpshead

Copy link
Copy Markdown
MemberAuthor

I realize we may simplify the text around this when we actually do the deprecation behavior change (which we could choose to do now that we're 3.15 though it's nicer to wait longer). But having the detail in the docs and the backport to the stable docs still seems good so I fixed this up. a backport to 3.13 docs will need to be manual due to the added method in 3.14.

gpshead added a commit that referenced this pull request Dec 21, 2025
… datetime. (GH-116179) (#143037)
gh-70647: Better promote how to safely parse yearless dates in datetime. (GH-116179)
* gh-70647: Better promote how to safely parse yearless dates in 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 doing
that could have other consequences for existing code. This documented
workaround *always* works.
* doctest code within note is bad, dedent.
* Update to match the error message.
* remove no longer referenced footnote
* ignore the warning in the doctest
* use Petr's suggestion for the docs to hide the warning processing
* cover date.strptime (3.14) as well
(cherry picked from commit b8d3fdd)
Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com>
gpshead added a commit to gpshead/cpython that referenced this pull request Dec 21, 2025
…datetime. (pythonGH-116179)
* pythongh-70647: Better promote how to safely parse yearless dates in 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 doing
that could have other consequences for existing code. This documented
workaround *always* works.
* doctest code within note is bad, dedent.
* Update to match the error message.
* remove no longer referenced footnote
* ignore the warning in the doctest
* use Petr's suggestion for the docs to hide the warning processing
* cover date.strptime (3.14) as well
@bedevere-app

Copy link
Copy Markdown

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

gpshead added a commit that referenced this pull request Dec 21, 2025
… datetime. (GH-116179) (#143038)
* gh-70647: Better promote how to safely parse yearless dates in datetime. (GH-116179)
* gh-70647: Better promote how to safely parse yearless dates in 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 doing
that could have other consequences for existing code. This documented
workaround *always* works.
* doctest code within note is bad, dedent.
* Update to match the error message.
* remove no longer referenced footnote
* ignore the warning in the doctest
* use Petr's suggestion for the docs to hide the warning processing
* cover date.strptime (3.14) as well
* remove date.strptime mentions from 3.14
cocolato pushed a commit to cocolato/cpython that referenced this pull request Dec 22, 2025
…datetime. (pythonGH-116179)
* pythongh-70647: Better promote how to safely parse yearless dates in 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 doing
that could have other consequences for existing code. This documented
workaround *always* works.
* doctest code within note is bad, dedent.
* Update to match the error message.
* remove no longer referenced footnote
* ignore the warning in the doctest
* use Petr's suggestion for the docs to hide the warning processing
* cover date.strptime (3.14) as well
lucc added a commit to lucc/khard that referenced this pull request Feb 18, 2026
Python uses 1900 as the default year if a partial date is parsed with
datetime.datetime.strptime. This fails for February 29th because 1900
is not a leap year.
This is discussed in #335 and upstream in
python/cpython#116179.
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docsDocumentation in the Doc dirskip news

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

7 participants

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