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: Misleading missing parenthesis syntax error
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, kermit666, mark.dickinson, serhiy.storchaka
Priority: normal Keywords:

Created on 2013-01-10 14:09 by kermit666, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (7)
msg179547 - (view) Author: Dražen Lučanin (kermit666) * Date: 2013-01-10 14:09
When running this script:

things = ['a', 'b']
things.append('c'
for a in things:
    print a

I get the following output:

$ python script.py 
  File "script.py", line 3
    for a in things:
                   ^
SyntaxError: invalid syntax

the SyntaxError is very misguiding. The error is in a missing parenthesis after the append call, but the error output points to the colon in the for loop. It got me looking for some invisible characters around the colon (which sometimes do pop up in my IPython notebook in OS X).

Expected behaviour - the interpreter should warn me that I have an unmatched parenthesis or at least give some hint as to what possible tokens were expected instead of a colon to help me identify the faulty expression. It is hard to match all parentheses as it is, let alone when the error caused by them shows up in a different line pointing to a token of a different expression with a very vague description.
msg179548 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-01-10 14:22
The colon is the first invalid char, for example this is valid python and works:
>>> things = ['a', 'b']
>>> things.append('c'
... for a in things)
>>> things
['a', 'b', <generator object <genexpr> at 0xb76dacfc>]

In your case Python expects a genexp like things.append('c' for a in things), and when it finds the ':' it gives error.
If you use e.g. a 'while', you will see the error earlier:
>>> things = ['a', 'b']
>>> things.append('c'
... while True:
  File "<stdin>", line 2
    while True:
        ^
SyntaxError: invalid syntax
msg179549 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2013-01-10 14:23
> The error is in a missing parenthesis after the append call

Well, that's one *possible* cause of the error.  But fixing that missing parenthesis isn't the only way to make the code correct, and Python doesn't have any reasonable way to guess which of the various possible errors you made or what you intended to write.  For example, the following is valid code:

things = ['a', 'b']
things.append('c'
for a in things)
msg179556 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-01-10 15:07
For better syntax error messages see issue1634034.
msg179557 - (view) Author: Dražen Lučanin (kermit666) * Date: 2013-01-10 15:13
Yes, sure, I agree with both comments. Fors are a bit disambiguous in
this context in comparison to while loops. But something in the style
of Ezio's explanation might come in handy in the error output.

e.g.

SyntaxError: invalid syntax. "," or ")" expected in the argument list
of the method append, but ':' found instead.

(or the equivalent that's possible on that syntactic tree level)

Something that might explain that it's some sort of list being parsed,
not a for-loop command. It would point the coder in the right
direction, speeding him up.

This would all be a bit simpler if an indent was obligatory after
inserting a newline in the middle of a command. Then this would pass
OK

things.append('c'
    for a in things)

while this would fail

things.append('c'
for a in things)

with a syntax error on the first line, because the second line
would've been interpreted as a new command, not a continuation of the
first one.

I don't know if something like that would be possible, due to some
other aspects, though...

On Thu, Jan 10, 2013 at 3:23 PM, Mark Dickinson <report@bugs.python.org> wrote:
>
> Mark Dickinson added the comment:
>
>> The error is in a missing parenthesis after the append call
>
> Well, that's one *possible* cause of the error.  But fixing that missing parenthesis isn't the only way to make the code correct, and Python doesn't have any reasonable way to guess which of the various possible errors you made or what you intended to write.  For example, the following is valid code:
>
> things = ['a', 'b']
> things.append('c'
> for a in things)
>
> ----------
> nosy: +mark.dickinson
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue16917>
> _______________________________________
msg179558 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-01-10 15:29
> This would all be a bit simpler if an indent was obligatory after
> inserting a newline in the middle of a command.

This is not such a bad idea, but it is not backward-compatible and it would be inconsistent with how parentheses works in other situations.

Developers usually learn that they should look back at least till the previous line while investigating syntax errors.  Even in C (and similar languages) it's common that errors reported on one line are caused by e.g. a missing ';' on the previous line.
msg179675 - (view) Author: Dražen Lučanin (kermit666) * Date: 2013-01-11 13:03
Yes, I see your point.

Well, for now I am voting for issue1634034 to get some sort of
resolution, as Serhiy mentioned, as it might help this scenario as
well.

Cheers!

On Thu, Jan 10, 2013 at 4:29 PM, Ezio Melotti <report@bugs.python.org> wrote:
>
> Ezio Melotti added the comment:
>
>> This would all be a bit simpler if an indent was obligatory after
>> inserting a newline in the middle of a command.
>
> This is not such a bad idea, but it is not backward-compatible and it would be inconsistent with how parentheses works in other situations.
>
> Developers usually learn that they should look back at least till the previous line while investigating syntax errors.  Even in C (and similar languages) it's common that errors reported on one line are caused by e.g. a missing ';' on the previous line.
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue16917>
> _______________________________________
History
Date User Action Args
2022-04-11 14:57:40adminsetgithub: 61121
2013-01-11 13:03:10kermit666setmessages: + msg179675
2013-01-10 15:29:39ezio.melottisetmessages: + msg179558
2013-01-10 15:13:32kermit666setmessages: + msg179557
2013-01-10 15:07:43serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg179556
2013-01-10 14:23:41mark.dickinsonsetnosy: + mark.dickinson
messages: + msg179549
2013-01-10 14:22:57ezio.melottisetstatus: open -> closed

nosy: + ezio.melotti
messages: + msg179548

resolution: not a bug
stage: resolved
2013-01-10 14:12:57kermit666settype: compile error -> behavior
2013-01-10 14:09:31kermit666create