From 8c11d9097260f6e8cb1b46118814095cfb186cb9 Mon Sep 17 00:00:00 2001 From: Vladimir Iakovlev Date: Wed, 10 May 2017 16:18:35 +0200 Subject: [PATCH] #17: Add support of dict unpacking --- lib/astunparse/unparser.py | 10 +++++++--- tests/common.py | 5 +++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/astunparse/unparser.py b/lib/astunparse/unparser.py index 71a9456..17323b3 100644 --- a/lib/astunparse/unparser.py +++ b/lib/astunparse/unparser.py @@ -561,9 +561,13 @@ def _Dict(self, t): self.write("{") def write_pair(pair): (k, v) = pair - self.dispatch(k) - self.write(": ") - self.dispatch(v) + if k is None: + self.write('**') + self.dispatch(v) + else: + self.dispatch(k) + self.write(": ") + self.dispatch(v) self.write(",") self._indent +=1 self.fill("") diff --git a/tests/common.py b/tests/common.py index f930308..500a0c1 100644 --- a/tests/common.py +++ b/tests/common.py @@ -327,6 +327,11 @@ def test_set_comprehension(self): def test_dict_comprehension(self): self.check_roundtrip("{x: x*x for x in range(10)}") + @unittest.skipIf(sys.version_info < (3, 6), "Not supported < 3.6") + def test_dict_with_unpacking(self): + self.check_roundtrip("{**x}") + self.check_roundtrip("{a: b, **x}") + @unittest.skipIf(sys.version_info < (3, 6), "Not supported < 3.6") def test_async_comp_and_gen_in_async_function(self): self.check_roundtrip(async_comprehensions_and_generators)