Skip to content

Commit 442dd8f

Browse files
gh-94192: Fix error for dictionary literals with invalid expression as value. (GH-94304)
* Fix error for dictionary literals with invalid expression as value. * Remove trailing whitespace (cherry picked from commit 8c237a7) Co-authored-by: wookie184 <wookie1840@gmail.com>
1 parent 1b27ec5 commit 442dd8f

File tree

4 files changed

+339
-263
lines changed

4 files changed

+339
-263
lines changed

Grammar/python.gram

+1-1
Original file line numberDiff line numberDiff line change
@@ -1314,4 +1314,4 @@ invalid_kvpair:
13141314
| a=expression !(':') {
13151315
RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError, a->lineno, a->end_col_offset - 1, a->end_lineno, -1, "':' expected after dictionary key") }
13161316
| expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "cannot use a starred expression in a dictionary value") }
1317-
| expression a=':' {RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }
1317+
| expression a=':' &('}'|',') {RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }

Lib/test/test_syntax.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -1265,12 +1265,22 @@
12651265
Traceback (most recent call last):
12661266
SyntaxError: expression expected after dictionary key and ':'
12671267
1268-
# Ensure that the error is not raise for syntax errors that happen after sets
1268+
# Ensure that the error is not raised for syntax errors that happen after sets
12691269
12701270
>>> {1} $
12711271
Traceback (most recent call last):
12721272
SyntaxError: invalid syntax
12731273
1274+
# Ensure that the error is not raised for invalid expressions
1275+
1276+
>>> {1: 2, 3: foo(,), 4: 5}
1277+
Traceback (most recent call last):
1278+
SyntaxError: invalid syntax
1279+
1280+
>>> {1: $, 2: 3}
1281+
Traceback (most recent call last):
1282+
SyntaxError: invalid syntax
1283+
12741284
Specialized indentation errors:
12751285
12761286
>>> while condition:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix error for dictionary literals with invalid expression as value.

0 commit comments

Comments
 (0)