Skip to content

The docs of "traceback.format_exception_only" is incorrect when the Exception has notes #109184

Closed
@Delgan

Description

@Delgan

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dirstdlibPython modules in the Lib dir

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions