Skip to content

Commit 4e289e2

Browse files
gh-92417: Update docs and examples of doctest.IGNORE_EXCEPTION_DETAIL for Py>=3 (GH-92502) (GH-92963)
(cherry picked from commit 97b9c10)
1 parent 56c8d7c commit 4e289e2

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
@@ -564,41 +564,35 @@ doctest decides whether actual output matches an example's expected output:
564564

565565
.. data:: IGNORE_EXCEPTION_DETAIL
566566

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

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

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

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

0 commit comments

Comments
 (0)