This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Misplaced diagnostic caret for some SyntaxErrors
Type: Stage: resolved
Components: Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: zach.ware, zwol
Priority: normal Keywords:

Created on 2015-02-25 02:58 by zwol, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg236560 - (view) Author: Zack Weinberg (zwol) * Date: 2015-02-25 02:58
I tripped over a couple of SyntaxError cases where the diagnostic caret is misplaced.

    >>> While x:
      File "<stdin>", line 1
        While x:
              ^
    SyntaxError: invalid syntax

The caret should point to the capital W in 'While'.

    >>> for x in the range(10):
      File "<stdin>", line 1
        for x in the range(10):
                         ^
    SyntaxError: invalid syntax

The caret should point to the 'the'.
msg236561 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2015-02-25 04:05
The caret is correct:

>>> While = 'some string'
>>> While
'some string'
>>> the = range(10)
>>> the
range(0, 10)
>>> for x in the: pass
... 
>>> 

In both cases, the error is having an expression followed by an expression, and the caret points at the second expression.  There's nothing wrong with 'While' and 'the', they're perfectly legitimate names, though rarely used.
msg236579 - (view) Author: Zack Weinberg (zwol) * Date: 2015-02-25 14:45
In terms of the formal grammar of the language, you are correct.  However, the position of the caret should be chosen based *not* on the formal grammar, but on a heuristic estimation of what the most probable mistake actually is.  In both of the cases I listed, the most probable mistake is as I described.  Please reconsider.
msg236582 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2015-02-25 15:13
The problem is, the interpreter can't know what you meant to do, so it can only point out what it can't compile.  We did recently add a special case for print without parentheses, but adding special cases for everything is just not feasible.  Besides, where should the caret point in each of these cases, why, and how would you explain it to the parser?

for x in the range(10):

for x in range(10) the:

for x in range(10) range(10):

for each x in range(10):

for x each in range(10):


Those are just some of the possibilities for just the for statement, let alone every other statement, or even every place an expression can be used.

If you can come up with a good, simple, general patch that covers every case, please do so!  I'm quite sure it would be accepted.  But until then, I'm afraid this isn't going to happen.
History
Date User Action Args
2022-04-11 14:58:13adminsetgithub: 67706
2015-02-25 15:13:42zach.waresetstatus: open -> closed

messages: + msg236582
2015-02-25 14:45:33zwolsetstatus: closed -> open

messages: + msg236579
2015-02-25 04:05:14zach.waresetstatus: open -> closed

nosy: + zach.ware
messages: + msg236561

resolution: not a bug
stage: resolved
2015-02-25 02:58:19zwolcreate