Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d07a1cb

Browse files
committed
Issue #26489: Add dictionary unpacking support to Tools/parser/unparse.py
Patch by Guo Ci Teo.
2 parents 48238c7 + d66dd5c commit d07a1cb

3 files changed

Lines changed: 20 additions & 3 deletions

File tree

Lib/test/test_tools/test_unparse.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ def test_with_as(self):
259259
def test_with_two_items(self):
260260
self.check_roundtrip(with_two_items)
261261

262+
def test_dict_unpacking_in_dict(self):
263+
# See issue 26489
264+
self.check_roundtrip(r"""{**{'y': 2}, 'x': 1}""")
265+
self.check_roundtrip(r"""{**{'y': 2}, **{'x': 1}}""")
266+
262267

263268
class DirectoryTestCase(ASTTestCase):
264269
"""Test roundtrip behaviour on all files in Lib and Lib/test."""

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,9 @@ Windows
828828
Tools/Demos
829829
-----------
830830

831+
- Issue #26489: Add dictionary unpacking support to Tools/parser/unparse.py.
832+
Patch by Guo Ci Teo.
833+
831834
- Issue #26316: Fix variable name typo in Argument Clinic.
832835

833836
- Issue #25440: Fix output of python-config --extension-suffix.

Tools/parser/unparse.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -456,12 +456,21 @@ def _Set(self, t):
456456

457457
def _Dict(self, t):
458458
self.write("{")
459-
def write_pair(pair):
460-
(k, v) = pair
459+
def write_key_value_pair(k, v):
461460
self.dispatch(k)
462461
self.write(": ")
463462
self.dispatch(v)
464-
interleave(lambda: self.write(", "), write_pair, zip(t.keys, t.values))
463+
464+
def write_item(item):
465+
k, v = item
466+
if k is None:
467+
# for dictionary unpacking operator in dicts {**{'y': 2}}
468+
# see PEP 448 for details
469+
self.write("**")
470+
self.dispatch(v)
471+
else:
472+
write_key_value_pair(k, v)
473+
interleave(lambda: self.write(", "), write_item, zip(t.keys, t.values))
465474
self.write("}")
466475

467476
def _Tuple(self, t):

0 commit comments

Comments
 (0)