New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
doctest exceptions include nondeterministic namespace #66899
Comments
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 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. |
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. |
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? |
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. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: