Skip to content

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

Closed

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

Activity

  1. added
    docs
    on Sep 9, 2023
  2. terryjreedy commented on Sep 9, 2023

    @terryjreedy
    Member
  3. added
    stdlib
    on Sep 9, 2023
  4. iritkatriel commented on Sep 9, 2023

    @iritkatriel
    Member

    Thank you. "Normally, the list contains a single string" is also no longer true.

  5. added a commit that references this issue on Sep 9, 2023
  6. iritkatriel commented on Sep 9, 2023

    @iritkatriel
    Member

    Is this better? #109201

  7. Delgan commented on Sep 9, 2023

    @Delgan
    ContributorAuthor

    Is this better? #109201

    Looks good and accurate! Thanks for the update. 👍

  8. added 4 commits that reference this issue on Sep 12, 2023
  9. added a commit that references this issue on Sep 13, 2023
  10. added a commit that references this issue on Sep 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsstdlib

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions