Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

gpshead
Copy link
Member

@gpshead gpshead commented Mar 1, 2024

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.
@gpshead gpshead added the docs Documentation in the Doc dir label Mar 1, 2024
@gpshead gpshead self-assigned this Mar 1, 2024
@gpshead gpshead marked this pull request as ready for review March 1, 2024 08:05
@gpshead gpshead requested review from pganssle and abalkin as code owners March 1, 2024 08:05
@encukou
Copy link
Member

encukou commented Apr 2, 2024

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
Contributor

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

@gpshead gpshead added needs backport to 3.12 only security fixes needs backport to 3.13 bugs and security fixes labels Mar 21, 2025
@gpshead gpshead moved this from Todo to In Progress in Docs PRs Mar 21, 2025
>>> from datetime import datetime
>>> import warnings
>>> value = "2/29"
>>> with warnings.catch_warnings():
Copy link
Member Author

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 manager

   import warnings
   catch_warnings = warnings.catch_warnings()
   catch_warnings.__enter__()

.. testcleanup::

   catch_warnings.__exit__()

.. doctest::

    >>> from datetime import datetime
    >>> import warnings
    >>> value = "2/29"
    >>> datetime.strptime(value, "%m/%d")
    Traceback (most recent call last):
    ...
    ValueError: day 29 must be in range 1..28 for month 2 in year 1900
    >>> datetime.strptime(f"1904 {value}", "%Y %m/%d")
    datetime.datetime(1904, 2, 29, 0, 0)

Copy link
Contributor

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.

@gpshead gpshead removed the needs backport to 3.12 only security fixes label Mar 21, 2025
@serhiy-storchaka serhiy-storchaka added the needs backport to 3.14 bugs and security fixes label May 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes skip news
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

7 participants