Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -4148,6 +4148,26 @@ def test_dont_swallow_subexceptions_of_falsey_exceptiongroup(self):
self.assertIn(f'{FalseyException.__name__}: {msg}', str_exc[-2])


def test_syntax_error_keyword_typo_preserves_lineno_type(self):
# gh-162296: formatting must not change lineno/end_lineno types.
try:
compile("whille True:\n pass\n", "example.py", "exec")
except SyntaxError as exc:
te = traceback.TracebackException.from_exception(exc)
self.assertIsInstance(te.lineno, str)
self.assertIsInstance(te.end_lineno, str)
self.assertEqual(te.lineno, '1')
self.assertEqual(te.end_lineno, '1')
''.join(te.format())
self.assertIsInstance(te.lineno, str)
self.assertIsInstance(te.end_lineno, str)
self.assertEqual(te.lineno, '1')
self.assertEqual(te.end_lineno, '1')
else:
self.fail("SyntaxError not raised")



global_for_suggestions = None


Expand Down
4 changes: 2 additions & 2 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -1523,8 +1523,8 @@ def _find_keyword_typos(self):
self.text = token.line
self.offset = token.start[1] + 1
self.end_offset = token.end[1] + 1
self.lineno = start[0]
self.end_lineno = end[0]
self.lineno = str(start[0])
self.end_lineno = str(end[0])
self.msg = f"invalid syntax. Did you mean '{suggestion}'?"
return

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :class:`traceback.TracebackException` so that formatting a
:exc:`SyntaxError` with a keyword typo suggestion preserves ``lineno``
and ``end_lineno`` as strings.
Loading