Skip to content

Commit bb4c167

Browse files
gh-111488: Changed error message in case of no 'in' keyword after 'for' in cmp (#113656)
1 parent bbf214d commit bb4c167

File tree

5 files changed

+1820
-1592
lines changed

5 files changed

+1820
-1592
lines changed

Grammar/python.gram

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,8 @@ for_if_clause[comprehension_ty]:
968968
CHECK_VERSION(comprehension_ty, 6, "Async comprehensions are", _PyAST_comprehension(a, b, c, 1, p->arena)) }
969969
| 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
970970
_PyAST_comprehension(a, b, c, 0, p->arena) }
971+
| 'async'? 'for' (bitwise_or (',' bitwise_or)* [',']) !'in' {
972+
RAISE_SYNTAX_ERROR("'in' expected after for-loop variables") }
971973
| invalid_for_target
972974

973975
listcomp[expr_ty]:

Lib/test/test_syntax.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,36 @@
259259
Traceback (most recent call last):
260260
SyntaxError: invalid syntax
261261
262+
Comprehensions without 'in' keyword:
263+
264+
>>> [x for x if range(1)]
265+
Traceback (most recent call last):
266+
SyntaxError: 'in' expected after for-loop variables
267+
268+
>>> tuple(x for x if range(1))
269+
Traceback (most recent call last):
270+
SyntaxError: 'in' expected after for-loop variables
271+
272+
>>> [x for x() in a]
273+
Traceback (most recent call last):
274+
SyntaxError: cannot assign to function call
275+
276+
>>> [x for a, b, (c + 1, d()) in y]
277+
Traceback (most recent call last):
278+
SyntaxError: cannot assign to expression
279+
280+
>>> [x for a, b, (c + 1, d()) if y]
281+
Traceback (most recent call last):
282+
SyntaxError: 'in' expected after for-loop variables
283+
284+
>>> [x for x+1 in y]
285+
Traceback (most recent call last):
286+
SyntaxError: cannot assign to expression
287+
288+
>>> [x for x+1, x() in y]
289+
Traceback (most recent call last):
290+
SyntaxError: cannot assign to expression
291+
262292
Comprehensions creating tuples without parentheses
263293
should produce a specialized error message:
264294

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ Eddy De Greef
666666
Duane Griffin
667667
Grant Griffin
668668
Andrea Griffini
669+
Semyon Grigoryev
669670
Duncan Grisby
670671
Olivier Grisel
671672
Fabian Groffen
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Changed error message in case of no 'in' keyword after 'for' in list
2+
comprehensions

0 commit comments

Comments
 (0)