Skip to content

Commit 511ee1c

Browse files
authored
[3.10] bpo-45727: Make the syntax error for missing comma more consistent (GH-29427) (GH-29647)
(cherry picked from commit 546cefc) Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
1 parent 82f1a6e commit 511ee1c

File tree

4 files changed

+268
-334
lines changed

4 files changed

+268
-334
lines changed

Grammar/python.gram

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ expressions[expr_ty]:
529529
| expression
530530
expression[expr_ty] (memo):
531531
| invalid_expression
532+
| invalid_legacy_expression
532533
| a=disjunction 'if' b=disjunction 'else' c=expression { _PyAST_IfExp(b, a, c, EXTRA) }
533534
| disjunction
534535
| lambdef
@@ -666,7 +667,6 @@ await_primary[expr_ty] (memo):
666667
| AWAIT a=primary { CHECK_VERSION(expr_ty, 5, "Await expressions are", _PyAST_Await(a, EXTRA)) }
667668
| primary
668669
primary[expr_ty]:
669-
| invalid_primary # must be before 'primay genexp' because of invalid_genexp
670670
| a=primary '.' b=NAME { _PyAST_Attribute(a, b->v.Name.id, Load, EXTRA) }
671671
| a=primary b=genexp { _PyAST_Call(a, CHECK(asdl_expr_seq*, (asdl_expr_seq*)_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
672672
| a=primary '(' b=[arguments] ')' {
@@ -856,11 +856,11 @@ invalid_legacy_expression:
856856
"Missing parentheses in call to '%U'. Did you mean %U(...)?", a->v.Name.id, a->v.Name.id) : NULL}
857857

858858
invalid_expression:
859-
| invalid_legacy_expression
860859
# !(NAME STRING) is not matched so we don't show this error with some invalid string prefixes like: kf"dsfsdf"
861860
# Soft keywords need to also be ignored because they can be parsed as NAME NAME
862861
| !(NAME STRING | SOFT_KEYWORD) a=disjunction b=expression_without_invalid {
863-
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Perhaps you forgot a comma?") }
862+
_PyPegen_check_legacy_stmt(p, a) ? NULL : RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b,
863+
"invalid syntax. Perhaps you forgot a comma?") }
864864
| a=disjunction 'if' b=disjunction !('else'|':') { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "expected 'else' after 'if' expression") }
865865

866866
invalid_named_expression:
@@ -902,8 +902,6 @@ invalid_del_stmt:
902902
RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }
903903
invalid_block:
904904
| NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
905-
invalid_primary:
906-
| primary a='{' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "invalid syntax") }
907905
invalid_comprehension:
908906
| ('[' | '(' | '{') a=starred_expression for_if_clauses {
909907
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") }

Lib/test/test_exceptions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,14 @@ def testSyntaxErrorOffset(self):
226226
check(b'Python = "\xcf\xb3\xf2\xee\xed" +', 1, 18)
227227
check('x = "a', 1, 5)
228228
check('lambda x: x = 2', 1, 1)
229-
check('f{a + b + c}', 1, 2)
229+
check('f{a + b + c}', 1, 1)
230230
check('[file for str(file) in []\n])', 1, 11)
231231
check('a = « hello » « world »', 1, 5)
232232
check('[\nfile\nfor str(file)\nin\n[]\n]', 3, 5)
233233
check('[file for\n str(file) in []]', 2, 2)
234234
check("ages = {'Alice'=22, 'Bob'=23}", 1, 16)
235235
check('match ...:\n case {**rest, "key": value}:\n ...', 2, 19)
236+
check("a b c d e f", 1, 1)
236237

237238
# Errors thrown by compile.c
238239
check('class foo:return 1', 1, 11)

0 commit comments

Comments
 (0)