Description
Documentation
The documentation of traceback.format_exception_only
states the following (emphasis mine):
Format the exception part of a traceback using an exception value such as given by
sys.last_value
. The return value is a list of strings, each ending in a newline. Normally, the list contains a single string; however, for SyntaxError exceptions, it contains several lines that (when printed) display detailed information about where the syntax error occurred. The message indicating which exception occurred is the always last string in the list.
The last string isn't always the formatted Exception
instance. Actually, the last string can be any arbitrary string if notes are added to the Exception
, as demonstrated by the following example:
import traceback
e = ValueError("The error")
e.add_note("Notes:\n1. Part 1\n2. Part 2")
output = traceback.format_exception_only(e)
print(output)
The output
is actually a list of 4 elements, with the message representing the raised Exception
being in first position:
['ValueError: The error\n', 'Notes:\n', '1. Part 1\n', '2. Part 2\n']
This also applies to the documentation of TracebackException.format_exception_only
.
Linked PRs
- gh-109184: update traceback module doc w.r.t notes (message is no longer always at the end) #109201
- [3.12] gh-109184: update traceback module doc w.r.t notes (message is no longer always at the end) (#109201) #109334
- [3.11] gh-109184: update traceback module doc w.r.t notes (message is no longer always at the end) (#109201) #109336