summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBerker Peksag <berker.peksag@gmail.com>2016-03-06 16:50:15 +0200
committerBerker Peksag <berker.peksag@gmail.com>2016-03-06 16:50:15 +0200
commitd66dd5ce68cbf4a33c385439d5eeb2bff4e860f1 (patch)
tree2a110e9d32aa7ad9aa1b0e44845628c526d11925
parente88dd1c32c2961e0fe40b09c48904451fa1eba9a (diff)
downloadcpython-git-d66dd5ce68cbf4a33c385439d5eeb2bff4e860f1.tar.gz
Issue #26489: Add dictionary unpacking support to Tools/parser/unparse.py
Patch by Guo Ci Teo.
-rw-r--r--Lib/test/test_tools/test_unparse.py5
-rw-r--r--Misc/NEWS3
-rw-r--r--Tools/parser/unparse.py15
3 files changed, 20 insertions, 3 deletions
diff --git a/Lib/test/test_tools/test_unparse.py b/Lib/test/test_tools/test_unparse.py
index 976a6c59ae..734bbc215a 100644
--- a/Lib/test/test_tools/test_unparse.py
+++ b/Lib/test/test_tools/test_unparse.py
@@ -250,6 +250,11 @@ class UnparseTestCase(ASTTestCase):
def test_with_two_items(self):
self.check_roundtrip(with_two_items)
+ def test_dict_unpacking_in_dict(self):
+ # See issue 26489
+ self.check_roundtrip(r"""{**{'y': 2}, 'x': 1}""")
+ self.check_roundtrip(r"""{**{'y': 2}, **{'x': 1}}""")
+
class DirectoryTestCase(ASTTestCase):
"""Test roundtrip behaviour on all files in Lib and Lib/test."""
diff --git a/Misc/NEWS b/Misc/NEWS
index 9a775ac527..81c7dc8c6d 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -328,6 +328,9 @@ Windows
Tools/Demos
-----------
+- Issue #26489: Add dictionary unpacking support to Tools/parser/unparse.py.
+ Patch by Guo Ci Teo.
+
- Issue #26316: Fix variable name typo in Argument Clinic.
diff --git a/Tools/parser/unparse.py b/Tools/parser/unparse.py
index c82857710b..285030e792 100644
--- a/Tools/parser/unparse.py
+++ b/Tools/parser/unparse.py
@@ -393,12 +393,21 @@ class Unparser:
def _Dict(self, t):
self.write("{")
- def write_pair(pair):
- (k, v) = pair
+ def write_key_value_pair(k, v):
self.dispatch(k)
self.write(": ")
self.dispatch(v)
- interleave(lambda: self.write(", "), write_pair, zip(t.keys, t.values))
+
+ def write_item(item):
+ k, v = item
+ if k is None:
+ # for dictionary unpacking operator in dicts {**{'y': 2}}
+ # see PEP 448 for details
+ self.write("**")
+ self.dispatch(v)
+ else:
+ write_key_value_pair(k, v)
+ interleave(lambda: self.write(", "), write_item, zip(t.keys, t.values))
self.write("}")
def _Tuple(self, t):