]> granicus.if.org Git - python/commitdiff
Issue #26489: Add dictionary unpacking support to Tools/parser/unparse.py
authorBerker Peksag <berker.peksag@gmail.com>
Sun, 6 Mar 2016 14:50:15 +0000 (16:50 +0200)
committerBerker Peksag <berker.peksag@gmail.com>
Sun, 6 Mar 2016 14:50:15 +0000 (16:50 +0200)
Patch by Guo Ci Teo.

Lib/test/test_tools/test_unparse.py
Misc/NEWS
Tools/parser/unparse.py

index 976a6c59ae10ba61f4f5270d482e6901a615bae6..734bbc215ad2c631a7e9f81a87b3db53d656490a 100644 (file)
@@ -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."""
index 9a775ac527cd684d5e62d6bc5c63c9e515465fbd..81c7dc8c6dee7435daa4e788634b3fb5a9b0cc80 100644 (file)
--- 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.
 
 
index c82857710b06e9ca8c875efde842358d234da20b..285030e792f466dda857529a9ca0b023770fafa3 100644 (file)
@@ -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):