Skip to content

Commit 15fde41

Browse files
committed
bpo-40715: Reject dict unpacking on dict comprehensions
1 parent 72e0aa2 commit 15fde41

File tree

3 files changed

+290
-190
lines changed

3 files changed

+290
-190
lines changed

Grammar/python.gram

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,11 +502,13 @@ dict[expr_ty]:
502502
| '{' a=[kvpairs] '}' { _Py_Dict(CHECK(_PyPegen_get_keys(p, a)),
503503
CHECK(_PyPegen_get_values(p, a)), EXTRA) }
504504
dictcomp[expr_ty]:
505-
| '{' a=kvpair b=for_if_clauses '}' { _Py_DictComp(a->key, a->value, b, EXTRA) }
505+
| '{' a=simple_kvpair b=for_if_clauses '}' { _Py_DictComp(a->key, a->value, b, EXTRA) }
506+
| invalid_dict_comprehension
506507
kvpairs[asdl_seq*]: a=','.kvpair+ [','] { a }
507508
kvpair[KeyValuePair*]:
508509
| '**' a=bitwise_or { _PyPegen_key_value_pair(p, NULL, a) }
509-
| a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) }
510+
| simple_kvpair
511+
simple_kvpair[KeyValuePair*]: a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) }
510512
for_if_clauses[asdl_seq*]:
511513
| for_if_clause+
512514
for_if_clause[comprehension_ty]:
@@ -657,6 +659,9 @@ invalid_block:
657659
invalid_comprehension:
658660
| ('[' | '(' | '{') a=starred_expression for_if_clauses {
659661
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") }
662+
invalid_dict_comprehension:
663+
| '{' a='**' bitwise_or for_if_clauses '}' {
664+
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "dict unpacking cannot be used in dict comprehension") }
660665
invalid_parameters:
661666
| param_no_default* (slash_with_default | param_with_default+) param_no_default {
662667
RAISE_SYNTAX_ERROR("non-default argument follows default argument") }

Lib/test/test_unpack_ex.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@
158158
...
159159
SyntaxError: iterable unpacking cannot be used in comprehension
160160
161+
>>> {**{} for a in [1]}
162+
Traceback (most recent call last):
163+
...
164+
SyntaxError: dict unpacking cannot be used in dict comprehension
165+
161166
# Pegen is better here.
162167
# Generator expression in function arguments
163168

0 commit comments

Comments
 (0)