Skip to content

Commit 7b74e41

Browse files
python#111488: Changed error message in case of no 'in' keyword after 'for' in list
comprehensions
1 parent acf4cf5 commit 7b74e41

File tree

5 files changed

+1718
-1591
lines changed

5 files changed

+1718
-1591
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' a=star_targets+ !'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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,20 @@
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+
262276
Comprehensions creating tuples without parentheses
263277
should produce a specialized error message:
264278

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ Eddy De Greef
665665
Duane Griffin
666666
Grant Griffin
667667
Andrea Griffini
668+
Semyon Grigoryev
668669
Duncan Grisby
669670
Olivier Grisel
670671
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)