This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: doctest exceptions include nondeterministic namespace
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.2, Python 3.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: mu_mind, r.david.murray
Priority: normal Keywords:

Created on 2014-10-23 09:04 by mu_mind, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg229864 - (view) Author: David Barnett (mu_mind) Date: 2014-10-23 09:04
doctests includes special exception processing support (described in https://docs.python.org/3/library/doctest.html#what-about-exceptions), but in python3 it seems to print the fully-qualified class name even for exception classes in the same module, which makes the expected output vary between py2 and py3.

For instance, given a file spam/eggs.py with these contents:
  class Error(Exception):
      """Spam, Bacon, and Spam
  
      >>> raise Error()
      Traceback (most recent call last):
          ...
      Error
      """
running the command
  python3 -m doctest spam/eggs.py
will fail expecting "eggs.Error" instead of just "Error".

If you instead invoke it with the command
  nosetests3 --with-doctest spam/eggs.py
it will fail expecting yet another name, "spam.eggs.Error".

It may be possible to work around issues like this using ELLIPSIS, but it makes the doctests harder to read and it really seems like at least exception classes defined in the same file should be able to match by just their short class name.
msg229879 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-10-23 15:09
It is not possible to work around it with ELLIPSIS.  Look for the flag 'IGNORE_EXCEPTION_DETAIL', which is mentioned in the section you reference.
msg229884 - (view) Author: David Barnett (mu_mind) Date: 2014-10-23 16:21
But… that doesn't help. It completely changes the semantics of the doctests. I have a bunch of doctests demonstrating different failures of the same exception class, and with IGNORE_EXCEPTION_DETAIL running my doctests to verify they're still correct is next to useless. Plus it's very noisy to slap "# doctest: +IGNORE_EXCEPTION_DETAIL" everywhere just to get not-pathological behavior.

And this isn't even just about py2/py3 interoperability. It's a problem for my py3-only project, which fails depending on how I invoke doctests. So it's not just something we can hack around until py2 goes away and be fine.

Can't a separate option be added for just the module name and enabled by default?
msg229885 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-10-23 17:04
Not to 2.7, since that would be a new feature.

In Python we do not consider the content of an error message (as opposed to the exception class itself) to be part of the API, so it is not surprising that doctest does not really support checking it across versions.  That said, I agree that it would be nice to have.  If you want to propose a feature patch you can reopen the issue and update the title.

A workaround would be to capture the exception and display its str.  This would have the advantage of not cluttering your doctests with the Traceback lines, at the cost of having a try/except in your code sample.
History
Date User Action Args
2022-04-11 14:58:09adminsetgithub: 66899
2014-10-23 17:04:58r.david.murraysetmessages: + msg229885
2014-10-23 16:21:16mu_mindsetmessages: + msg229884
2014-10-23 15:09:02r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg229879

resolution: not a bug
stage: resolved
2014-10-23 09:04:37mu_mindcreate