python / python/cpython

TracebackException.format_exception_only() mutates attributes on keyword typo suggestion

Open
#154,544 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug description

TracebackException.format_exception_only() is not idempotent when _find_keyword_typos() finds a suggestion. The method directly mutates self.text, self.offset, self.end_offset, self.lineno, self.end_lineno, and self.msg, so a second call produces different output (e.g. wrong line number).

import traceback

code = "fr x in range(10):\n    pass\n"
try:
    compile(code, "<test>", "exec")
except SyntaxError as exc:
    te = traceback.TracebackException(type(exc), exc, None)
    r1 = list(te.format_exception_only())  # correct
    r2 = list(te.format_exception_only())  # different output
    assert r1 == r2  # fails

_find_keyword_typos was added in gh-132449.

CPython versions tested on

main

Operating systems tested on

macOS

Linked PRs
  • gh-154545

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at traceback.TracebackException.format_exception_only() and the _find_keyword_typos() entry point described in the issue. Reproduce the provided SyntaxError example, call format_exception_only() twice, and compare the results. Done means repeated calls produce identical output, including the line number and other exception details.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.