bpo-43822: Improve syntax errors for missing commas (GH-25377) · python/cpython@b280248

GitHub

File tree

Doc/library

Grammar

Include

Lib

test

Misc/NEWS.d/next/Core and Builtins

Parser

Tools/peg_generator/pegen

Original file line numberDiff line numberDiff line change@@ -59,6 +59,7 @@ AWAIT

5959ASYNC

6060TYPE_IGNORE

6161TYPE_COMMENT

62+SOFT_KEYWORD

6263ERRORTOKEN

63646465# These aren't used by the C tokenizer but are needed for tokenize.py

Original file line numberDiff line numberDiff line change@@ -7,6 +7,7 @@ _PyPegen_parse(Parser *p)

77 // Initialize keywords

88 p->keywords = reserved_keywords;

99 p->n_keyword_lists = n_keyword_lists;

10+ p->soft_keywords = soft_keywords;

10111112 // Run parser

1213 void *result = NULL;

@@ -459,6 +460,7 @@ expressions[expr_ty]:

459460 | a=expression ',' { _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) }

460461 | expression

461462expression[expr_ty] (memo):

463+ | invalid_expression

462464 | a=disjunction 'if' b=disjunction 'else' c=expression { _PyAST_IfExp(b, a, c, EXTRA) }

463465 | disjunction

464466 | lambdef

@@ -778,6 +780,13 @@ invalid_kwarg:

778780 | expression a='=' {

779781 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(

780782 a, "expression cannot contain assignment, perhaps you meant \"==\"?") }

783+784+invalid_expression:

785+ # !(NAME STRING) is not matched so we don't show this error with some invalid string prefixes like: kf"dsfsdf"

786+ # Soft keywords need to also be ignored because they can be parsed as NAME NAME

787+ | !(NAME STRING | SOFT_KEYWORD) a=disjunction expression {

788+ RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError, a->lineno, a->end_col_offset - 1, "invalid syntax. Perhaps you forgot a comma?") }

789+781790invalid_named_expression:

782791 | a=expression ':=' expression {

783792 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(

Original file line numberDiff line numberDiff line change@@ -103,7 +103,7 @@

103103 >>> dict(a = i for i in range(10))

104104 Traceback (most recent call last):

105105 ...

106- SyntaxError: invalid syntax

106+ SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

107107108108Verify that parenthesis are required when used as a keyword argument value

109109Original file line numberDiff line numberDiff line change@@ -248,22 +248,36 @@

248248249249# Missing commas in literals collections should not

250250# produce special error messages regarding missing

251-# parentheses

251+# parentheses, but about missing commas instead

252252253253>>> [1, 2 3]

254254Traceback (most recent call last):

255-SyntaxError: invalid syntax

255+SyntaxError: invalid syntax. Perhaps you forgot a comma?

256256257257>>> {1, 2 3}

258258Traceback (most recent call last):

259-SyntaxError: invalid syntax

259+SyntaxError: invalid syntax. Perhaps you forgot a comma?

260260261261>>> {1:2, 2:5 3:12}

262262Traceback (most recent call last):

263-SyntaxError: invalid syntax

263+SyntaxError: invalid syntax. Perhaps you forgot a comma?

264264265265>>> (1, 2 3)

266266Traceback (most recent call last):

267+SyntaxError: invalid syntax. Perhaps you forgot a comma?

268+269+# Make sure soft keywords constructs don't raise specialized

270+# errors regarding missing commas

271+272+>>> match x:

273+... y = 3

274+Traceback (most recent call last):

275+SyntaxError: invalid syntax

276+277+>>> match x:

278+... case y:

279+... 3 $ 3

280+Traceback (most recent call last):

267281SyntaxError: invalid syntax

268282269283From compiler_complex_args():

@@ -864,7 +878,7 @@

864878 SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?

865879866880Ensure that early = are not matched by the parser as invalid comparisons

867- >>> f(2, 4, x=34); {1,2 a}

881+ >>> f(2, 4, x=34); 1 $ 2

868882 Traceback (most recent call last):

869883 SyntaxError: invalid syntax

870884Original file line numberDiff line numberDiff line change@@ -0,0 +1,2 @@

1+Improve syntax errors in the parser for missing commas between expressions.

2+Patch by Pablo Galindo.