Skip to content

Commit

Permalink
gh-94192: Fix error for dictionary literals with invalid expression a…
Browse files Browse the repository at this point in the history
…s value. (#94304)

* Fix error for dictionary literals with invalid expression as value.

* Remove trailing whitespace
  • Loading branch information
wookie184 committed Jun 26, 2022
1 parent aedb519 commit 8c237a7
Show file tree
Hide file tree
Showing 4 changed files with 339 additions and 263 deletions.
2 changes: 1 addition & 1 deletion Grammar/python.gram
Expand Up @@ -1314,4 +1314,4 @@ invalid_kvpair:
| a=expression !(':') {
RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError, a->lineno, a->end_col_offset - 1, a->end_lineno, -1, "':' expected after dictionary key") }
| expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "cannot use a starred expression in a dictionary value") }
| expression a=':' {RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }
| expression a=':' &('}'|',') {RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }
12 changes: 11 additions & 1 deletion Lib/test/test_syntax.py
Expand Up @@ -1265,12 +1265,22 @@
Traceback (most recent call last):
SyntaxError: expression expected after dictionary key and ':'
# Ensure that the error is not raise for syntax errors that happen after sets
# Ensure that the error is not raised for syntax errors that happen after sets
>>> {1} $
Traceback (most recent call last):
SyntaxError: invalid syntax
# Ensure that the error is not raised for invalid expressions
>>> {1: 2, 3: foo(,), 4: 5}
Traceback (most recent call last):
SyntaxError: invalid syntax
>>> {1: $, 2: 3}
Traceback (most recent call last):
SyntaxError: invalid syntax
Specialized indentation errors:
>>> while condition:
Expand Down
@@ -0,0 +1 @@
Fix error for dictionary literals with invalid expression as value.

0 comments on commit 8c237a7

Please sign in to comment.