gh-111488: Changed error message in case of no 'in' keyword after 'for' in cmp (#113656)

This commit is contained in:
Grigoriev Semyon 2024-01-06 13:27:49 +03:00 committed by GitHub
parent bbf214df23
commit bb4c167060
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 3082 additions and 2854 deletions

View File

@ -968,6 +968,8 @@ for_if_clause[comprehension_ty]:
CHECK_VERSION(comprehension_ty, 6, "Async comprehensions are", _PyAST_comprehension(a, b, c, 1, p->arena)) } CHECK_VERSION(comprehension_ty, 6, "Async comprehensions are", _PyAST_comprehension(a, b, c, 1, p->arena)) }
| 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* { | 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
_PyAST_comprehension(a, b, c, 0, p->arena) } _PyAST_comprehension(a, b, c, 0, p->arena) }
| 'async'? 'for' (bitwise_or (',' bitwise_or)* [',']) !'in' {
RAISE_SYNTAX_ERROR("'in' expected after for-loop variables") }
| invalid_for_target | invalid_for_target
listcomp[expr_ty]: listcomp[expr_ty]:

View File

@ -259,6 +259,36 @@ SyntaxError: expected ':'
Traceback (most recent call last): Traceback (most recent call last):
SyntaxError: invalid syntax SyntaxError: invalid syntax
Comprehensions without 'in' keyword:
>>> [x for x if range(1)]
Traceback (most recent call last):
SyntaxError: 'in' expected after for-loop variables
>>> tuple(x for x if range(1))
Traceback (most recent call last):
SyntaxError: 'in' expected after for-loop variables
>>> [x for x() in a]
Traceback (most recent call last):
SyntaxError: cannot assign to function call
>>> [x for a, b, (c + 1, d()) in y]
Traceback (most recent call last):
SyntaxError: cannot assign to expression
>>> [x for a, b, (c + 1, d()) if y]
Traceback (most recent call last):
SyntaxError: 'in' expected after for-loop variables
>>> [x for x+1 in y]
Traceback (most recent call last):
SyntaxError: cannot assign to expression
>>> [x for x+1, x() in y]
Traceback (most recent call last):
SyntaxError: cannot assign to expression
Comprehensions creating tuples without parentheses Comprehensions creating tuples without parentheses
should produce a specialized error message: should produce a specialized error message:

View File

@ -666,6 +666,7 @@ Eddy De Greef
Duane Griffin Duane Griffin
Grant Griffin Grant Griffin
Andrea Griffini Andrea Griffini
Semyon Grigoryev
Duncan Grisby Duncan Grisby
Olivier Grisel Olivier Grisel
Fabian Groffen Fabian Groffen

View File

@ -0,0 +1,2 @@
Changed error message in case of no 'in' keyword after 'for' in list
comprehensions

5901
Parser/parser.c generated

File diff suppressed because it is too large Load Diff