gh-111159: Fix doctest output comparison for exceptions with notes (#111160)

This commit is contained in:
Nikita Sobolev 2023-10-21 21:02:00 +03:00 committed by GitHub
parent 5e7727b052
commit fd60549c0a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 159 additions and 1 deletions

View file

@ -1393,7 +1393,20 @@ class DocTestRunner:
# The example raised an exception: check if it was expected.
else:
exc_msg = traceback.format_exception_only(*exception[:2])[-1]
formatted_ex = traceback.format_exception_only(*exception[:2])
if issubclass(exception[0], SyntaxError):
# SyntaxError / IndentationError is special:
# we don't care about the carets / suggestions / etc
# We only care about the error message and notes.
# They start with `SyntaxError:` (or any other class name)
exc_msg_index = next(
index
for index, line in enumerate(formatted_ex)
if line.startswith(f"{exception[0].__name__}:")
)
formatted_ex = formatted_ex[exc_msg_index:]
exc_msg = "".join(formatted_ex)
if not quiet:
got += _exception_traceback(exception)