python / cpython

The Python programming language
https://www.python.org
Other
62.35k stars 29.94k forks source link

inconsistent behavior of tuple without parenthesis between tuple and list comprehensions and for statement #102771

Open luoxzhg opened 1 year ago

luoxzhg commented 1 year ago

Bug report

tuple without parenthesis in for statement is allowed

for i in 1,2,3: print(i)

output

1
2

but in list comprehensions, it is SyntaxError

[i for i in 1,2,3]

output

  File "<stdin>", line 1
    [i for i in 1, 2, 3]
                 ^
SyntaxError: invalid syntax

tuple comprehensions is same as this.

Your environment

corona10 commented 1 year ago

I think that it can be a conflict with the following case.

https://github.com/python/cpython/blob/4f5774f648eafd1a7076ecf9af9629fb81baa363/Lib/test/test_grammar.py#L1797

iritkatriel commented 1 year ago

This is only a bug if the behaviour contradicts the documentation. Does it?

iritkatriel commented 3 months ago

CC @pablogsal

pablogsal commented 3 months ago

I think this is ambiguous and that's why we don't allow it. I will investigate it with more care to see what we can do

pablogsal commented 3 months ago

Ah if I recall correctly this is to not make it inconsistent with generator comprehensions where using them directly in a function call is ambiguous:

foo(x for (x,y) in X, Y)