bpo-32012: Disallow trailing comma after genexpr without parenthesis.… · python/cpython@9165f77 · GitHub
Skip to content

Commit 9165f77

Browse files
bpo-32012: Disallow trailing comma after genexpr without parenthesis. (#4382)
1 parent 3bda022 commit 9165f77

4 files changed

Lines changed: 45 additions & 13 deletions

File tree

Doc/whatsnew/3.7.rst

Lines changed: 14 additions & 0 deletions

Lib/test/test_syntax.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,32 @@
125125
126126
From ast_for_call():
127127
128-
>>> def f(it, *varargs):
128+
>>> def f(it, *varargs, **kwargs):
129129
... return list(it)
130130
>>> L = range(10)
131131
>>> f(x for x in L)
132132
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
133133
>>> f(x for x in L, 1)
134134
Traceback (most recent call last):
135-
SyntaxError: Generator expression must be parenthesized if not sole argument
135+
SyntaxError: Generator expression must be parenthesized
136+
>>> f(x for x in L, y=1)
137+
Traceback (most recent call last):
138+
SyntaxError: Generator expression must be parenthesized
139+
>>> f(x for x in L, *[])
140+
Traceback (most recent call last):
141+
SyntaxError: Generator expression must be parenthesized
142+
>>> f(x for x in L, **{})
143+
Traceback (most recent call last):
144+
SyntaxError: Generator expression must be parenthesized
145+
>>> f(L, x for x in L)
146+
Traceback (most recent call last):
147+
SyntaxError: Generator expression must be parenthesized
136148
>>> f(x for x in L, y for y in L)
137149
Traceback (most recent call last):
138-
SyntaxError: Generator expression must be parenthesized if not sole argument
150+
SyntaxError: Generator expression must be parenthesized
151+
>>> f(x for x in L,)
152+
Traceback (most recent call last):
153+
SyntaxError: Generator expression must be parenthesized
139154
>>> f((x for x in L), 1)
140155
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
141156
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SyntaxError is now correctly raised when a generator expression without
2+
parenthesis is passed as an argument, but followed by a trailing comma.
3+
A generator expression always needs to be directly inside a set of parentheses
4+
and cannot have a comma on either side.

Python/ast.c

Lines changed: 9 additions & 10 deletions

0 commit comments

Comments
 (0)