Skip to content

Commit 97b9c10

Browse files
authored
gh-92417: Update docs and examples of doctest.IGNORE_EXCEPTION_DETAIL for Py>=3 (GH-92502)
1 parent 30deeac commit 97b9c10

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

Doc/library/doctest.rst

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -563,41 +563,35 @@ doctest decides whether actual output matches an example's expected output:
563563

564564
.. data:: IGNORE_EXCEPTION_DETAIL
565565

566-
When specified, an example that expects an exception passes if an exception of
567-
the expected type is raised, even if the exception detail does not match. For
568-
example, an example expecting ``ValueError: 42`` will pass if the actual
569-
exception raised is ``ValueError: 3*14``, but will fail, e.g., if
570-
:exc:`TypeError` is raised.
566+
When specified, doctests expecting exceptions pass so long as an exception
567+
of the expected type is raised, even if the details
568+
(message and fully-qualified exception name) don't match.
571569

572-
It will also ignore the module name used in Python 3 doctest reports. Hence
573-
both of these variations will work with the flag specified, regardless of
574-
whether the test is run under Python 2.7 or Python 3.2 (or later versions)::
570+
For example, an example expecting ``ValueError: 42`` will pass if the actual
571+
exception raised is ``ValueError: 3*14``, but will fail if, say, a
572+
:exc:`TypeError` is raised instead.
573+
It will also ignore any fully-qualified name included before the
574+
exception class, which can vary between implementations and versions
575+
of Python and the code/libraries in use.
576+
Hence, all three of these variations will work with the flag specified:
575577

576-
>>> raise CustomError('message')
578+
.. code-block:: pycon
579+
580+
>>> raise Exception('message')
577581
Traceback (most recent call last):
578-
CustomError: message
582+
Exception: message
579583
580-
>>> raise CustomError('message')
584+
>>> raise Exception('message')
581585
Traceback (most recent call last):
582-
my_module.CustomError: message
586+
builtins.Exception: message
583587
584-
Note that :const:`ELLIPSIS` can also be used to ignore the
585-
details of the exception message, but such a test may still fail based
586-
on whether or not the module details are printed as part of the
587-
exception name. Using :const:`IGNORE_EXCEPTION_DETAIL` and the details
588-
from Python 2.3 is also the only clear way to write a doctest that doesn't
589-
care about the exception detail yet continues to pass under Python 2.3 or
590-
earlier (those releases do not support :ref:`doctest directives
591-
<doctest-directives>` and ignore them as irrelevant comments). For example::
592-
593-
>>> (1, 2)[3] = 'moo'
588+
>>> raise Exception('message')
594589
Traceback (most recent call last):
595-
File "<stdin>", line 1, in <module>
596-
TypeError: object doesn't support item assignment
590+
__main__.Exception: message
597591
598-
passes under Python 2.3 and later Python versions with the flag specified,
599-
even though the detail
600-
changed in Python 2.4 to say "does not" instead of "doesn't".
592+
Note that :const:`ELLIPSIS` can also be used to ignore the
593+
details of the exception message, but such a test may still fail based
594+
on whether the module name is present or matches exactly.
601595

602596
.. versionchanged:: 3.2
603597
:const:`IGNORE_EXCEPTION_DETAIL` now also ignores any information relating

0 commit comments

Comments
 (0)