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: Incorrect "Perhaps you forgot a comma" hint
Type: Stage: resolved
Components: Parser Versions: Python 3.11, Python 3.10
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: aroberge, lys.nikolaou, pablogsal
Priority: normal Keywords:

Created on 2021-11-13 17:00 by aroberge, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg406287 - (view) Author: Andre Roberge (aroberge) * Date: 2021-11-13 17:00
Python 3.10 and 3.11:

>>> sum[i for i in [1, 2, 3] if i%2==0]
  File "<stdin>", line 1
    sum[i for i in [1, 2, 3] if i%2==0]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?

Furthermore, I don't find that highlighting the entire statement (or parts of it, if we use print(sum[...]) is very useful in attempting to find the source of the error.

In previous versions, we would get the following:

>>> sum[i for i in [1, 2, 3] if i%2==0]
  File "<stdin>", line 1
    sum[i for i in [1, 2, 3] if i%2==0]
          ^
SyntaxError: invalid syntax
msg406292 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-11-13 20:00
I think this is correct because if you add a comma, indeed is valid syntax:

>>> sum,[i for i in [1, 2, 3] if i%2==0]
(<built-in function sum>, [2])

Here the problem is that you are mentally mapping the construct to sum(...) where the parens are substituted with brackets, which is not what the parser sees, the parser sees two expressions glued together like

>> f() g()
  File "<stdin>", line 1
    f() g()
    ^^^^^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?

and it surrounds the whole expression. 

Do you have an idea on what you would prefer here? Otherwise, I suggest to close as "not a bug".
msg406295 - (view) Author: Andre Roberge (aroberge) * Date: 2021-11-13 20:13
I understand.

I reported this issue when one of my newest tests failed with Python 3.10 and 3.11. Actually, using friendly-traceback, using the location of the exception as indicated by cPython 3.10 and 3.11, here's part of the explanation it gives:

    The following lines of code would not cause any `SyntaxError`:

        sum + [i for i in [1, 2, 3] if i%2==0]
        sum - [i for i in [1, 2, 3] if i%2==0]
        sum * [i for i in [1, 2, 3] if i%2==0]
        sum, [i for i in [1, 2, 3] if i%2==0]
    Note: these are just some of the possible choices and that
    some of them might raise other types of exceptions.

So, I agree with you that suggesting a comma would be appropriate.  (I also miss the suggestion of inserting an equal sign above).

Meanwhile, with prior versions of cPython, here's the suggestion that was offered:

    You used square brackets, `[...]` instead of parentheses.
    Write the following instead:

        sum(i for i in [1, 2, 3] if i%2==0)

So, since using the suggestion currently by cPython (3.10, 3.11), one could get a syntactically valid statement by adding a comma, I cannot really argue that this is a bug. (Sorry, I should have checked in more details before.)

Therefore, I agree that this issue should probably be closed ... unless you find that suggesting a missing comma while there are many other possible operators that could be inserted could be considered as misleading.
msg406298 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-11-13 21:09
I will close this but will try to think for another issue into an error for the parens -> braket. Thanks for opening the issue!
History
Date User Action Args
2022-04-11 14:59:52adminsetgithub: 89959
2021-11-13 21:09:49pablogsalsetstatus: open -> closed
resolution: not a bug
messages: + msg406298

stage: resolved
2021-11-13 20:13:56arobergesetmessages: + msg406295
2021-11-13 20:00:21pablogsalsetmessages: + msg406292
2021-11-13 17:00:06arobergecreate