Skip to content

Commit 6330e73

Browse files
committed
use Petr's suggestion for the docs to hide the warning processing
1 parent 020ac63 commit 6330e73

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

Doc/library/datetime.rst

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2661,15 +2661,27 @@ will be pulled from the default value.
26612661
*not* a leap year. Always add a default leap year to partial date strings
26622662
before parsing.
26632663

2664+
2665+
.. testsetup::
2666+
2667+
# doctest seems to turn the warning into an error which makes it
2668+
# show up and require matching and prevents the actual interesting
2669+
# exception from being raised.
2670+
# Manually apply the catch_warnings context manager
2671+
import warnings
2672+
catch_warnings = warnings.catch_warnings()
2673+
catch_warnings.__enter__()
2674+
warnings.simplefilter("ignore")
2675+
2676+
.. testcleanup::
2677+
2678+
catch_warnings.__exit__()
2679+
26642680
.. doctest::
26652681

26662682
>>> from datetime import datetime
2667-
>>> import warnings
26682683
>>> value = "2/29"
2669-
>>> with warnings.catch_warnings():
2670-
... warnings.simplefilter("ignore")
2671-
... datetime.strptime(value, "%m/%d")
2672-
...
2684+
>>> datetime.strptime(value, "%m/%d")
26732685
Traceback (most recent call last):
26742686
...
26752687
ValueError: day 29 must be in range 1..28 for month 2 in year 1900

0 commit comments

Comments
 (0)