Skip to content

Commit 0ae7284

Browse files
authored
[3.10] gh-94192: Fix error for dictionary literals with invalid expression as value. (GH-94304) (#94344)
Co-authored-by: wookie184 <wookie1840@gmail.com>
1 parent da6f859 commit 0ae7284

File tree

4 files changed

+297
-221
lines changed

4 files changed

+297
-221
lines changed

Grammar/python.gram

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

Lib/test/test_syntax.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -970,12 +970,22 @@
970970
Traceback (most recent call last):
971971
SyntaxError: expression expected after dictionary key and ':'
972972
973-
# Ensure that the error is not raise for syntax errors that happen after sets
973+
# Ensure that the error is not raised for syntax errors that happen after sets
974974
975975
>>> {1} $
976976
Traceback (most recent call last):
977977
SyntaxError: invalid syntax
978978
979+
# Ensure that the error is not raised for invalid expressions
980+
981+
>>> {1: 2, 3: foo(,), 4: 5}
982+
Traceback (most recent call last):
983+
SyntaxError: invalid syntax
984+
985+
>>> {1: $, 2: 3}
986+
Traceback (most recent call last):
987+
SyntaxError: invalid syntax
988+
979989
Specialized indentation errors:
980990
981991
>>> while condition:
@@ -1508,8 +1518,8 @@ def fib(n):
15081518
self.assertEqual(compile(s1, '<string>', 'exec'), compile(s2, '<string>', 'exec'))
15091519
except SyntaxError:
15101520
self.fail("Indented statement over multiple lines is valid")
1511-
1512-
def test_continuation_bad_indentation(self):
1521+
1522+
def test_continuation_bad_indentation(self):
15131523
# Check that code that breaks indentation across multiple lines raises a syntax error
15141524

15151525
code = r"""\
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)