Skip to content
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

Closed
mumind mannequin opened this issue Oct 23, 2014 · 4 comments
Closed

doctest exceptions include nondeterministic namespace #66899

mumind mannequin opened this issue Oct 23, 2014 · 4 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@mumind
Copy link
Mannequin

mumind mannequin commented Oct 23, 2014

BPO 22710
Nosy @bitdancer

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:

assignee = None
closed_at = <Date 2014-10-23.15:09:02.674>
created_at = <Date 2014-10-23.09:04:37.651>
labels = ['invalid', 'type-bug', 'library']
title = 'doctest exceptions include nondeterministic namespace'
updated_at = <Date 2014-10-23.17:04:58.059>
user = 'https://bugs.python.org/mumind'

bugs.python.org fields:

activity = <Date 2014-10-23.17:04:58.059>
actor = 'r.david.murray'
assignee = 'none'
closed = True
closed_date = <Date 2014-10-23.15:09:02.674>
closer = 'r.david.murray'
components = ['Library (Lib)']
creation = <Date 2014-10-23.09:04:37.651>
creator = 'mu_mind'
dependencies = []
files = []
hgrepos = []
issue_num = 22710
keywords = []
message_count = 4.0
messages = ['229864', '229879', '229884', '229885']
nosy_count = 2.0
nosy_names = ['r.david.murray', 'mu_mind']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue22710'
versions = ['Python 3.2', 'Python 3.4']

@mumind
Copy link
Mannequin Author

mumind mannequin commented Oct 23, 2014

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.

@mumind mumind mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Oct 23, 2014
@bitdancer
Copy link
Member

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.

@mumind
Copy link
Mannequin Author

mumind mannequin commented Oct 23, 2014

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?

@bitdancer
Copy link
Member

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.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant