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
SyntaxError reports incorrect line number in error message vs displayed code #149007
SyntaxError reports incorrect line number in error message vs displayed code
Description
When a syntax error occurs in a multi-line statement, Python reports one line number in the error message but displays a different line in the code snippet. This creates confusion for users trying to locate and fix the problem.
Python version: Python 3.14.3
Note: This issue appears specifically when importing the file containing the syntax error, not when running it directly.
Traceback (most recent call last):
File "<string>", line 1, in <module>
import bug
File "/tmp/bug.py", line 16
num = len(streams)
^^^
SyntaxError: invalid syntax. Did you mean 'False'?
The error message reports line 16, but the displayed code shows line 6 (num = len(streams)).
Expected Behavior
The error message should report the same line number as the code displayed, or display the code from the reported line number, like when running the file directly using python bug.py
File "/tmp/bug.py", line 16
num=num
^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?
Root Cause
The actual syntax error is a missing comma on line 16 after num=num, but Python's import error reporting mechanism displays code from a different line, creating a mismatch.
Impact
This inconsistency makes debugging more difficult, as users must manually search for the actual problem rather than being directed to it.
Activity