You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TracebackException.lineno changes from str to int after format() when a keyword typo suggestion is found #162296
For a SyntaxError, TracebackException stores lineno and end_lineno as strings. When the keyword typo suggestion ("Did you mean 'while'?") triggers during formatting, both attributes are replaced with ints.
3.16.0a2+dev (remotes/origin/HEAD:d456158, Nov 19 2026, 22:08:35) [GCC 13.3.0]
before format(): '1' '1'
after format(): 1 1
On 3.14:
3.14.6 (main, Jul 18 2026, 17:01:31) [Clang 22.1.3 ]
before format(): '1' '1'
after format(): 1 1
On 3.13.18 both stay '1', since there are no keyword suggestions there.
The constructor does self.lineno = str(lno), but TracebackException._find_keyword_typos() later assigns self.lineno = start[0] and self.end_lineno = end[0], which are ints from the tokenizer.
The printed traceback is fine, so most people won't notice this. It only shows up if your code reads the attributes directly. For example, te.lineno == '1' is True before calling format() and False after.
It also affects equality: two TracebackException objects created from the same exception compare equal, but stop being equal after you format one of them.
I think the two assignments should be wrapped in str() to match the constructor. I'm happy to send a PR with a test if that's the right direction. The docs don't currently state the type of these attributes, so that could be clarified at the same time.
When a keyword suggestion is applied, format()also overwritesmsg, text, offset and end_offset. A formatted and an unformatted TracebackException therefore compare unequal either way, and str() on the two assignments doesn't change that.
So there are two things here:
lineno/end_lineno change type. That's a two line fix.
format() mutates the instance at all when a suggestion is found.
Is 2 intended, or should format() leave the instance untouched and compute the suggestion separately? If it should be fixed, would you prefer it handled in this issue or in a new one?
Activity
simonnelse commentedon Nov 20, 2026
Correction:
When a keyword suggestion is applied,
format()also overwritesmsg,text,offsetandend_offset. A formatted and an unformattedTracebackExceptiontherefore compare unequal either way, andstr()on the two assignments doesn't change that.So there are two things here:
lineno/end_linenochange type. That's a two line fix.format()mutates the instance at all when a suggestion is found.Is 2 intended, or should
format()leave the instance untouched and compute the suggestion separately? If it should be fixed, would you prefer it handled in this issue or in a new one?