Skip to content
Prev Previous commit
Next Next commit
use Petr's suggestion for the docs to hide the warning processing
  • Loading branch information
gpshead committed Dec 21, 2025
commit 6330e733ab64d731213fd0c6f970729b66aee317
22 changes: 17 additions & 5 deletions Doc/library/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2661,15 +2661,27 @@ will be pulled from the default value.
*not* a leap year. Always add a default leap year to partial date strings
before parsing.


.. 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__()
warnings.simplefilter("ignore")

.. testcleanup::

catch_warnings.__exit__()

.. doctest::

>>> from datetime import datetime
>>> import warnings
>>> value = "2/29"
>>> with warnings.catch_warnings():
... warnings.simplefilter("ignore")
... datetime.strptime(value, "%m/%d")
...
>>> 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
Expand Down
Loading