]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-92671: Don't omit parentheses when unparsing empty tuples (GH-92673)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 16 May 2022 13:01:34 +0000 (06:01 -0700)
committerGitHub <noreply@github.com>
Mon, 16 May 2022 13:01:34 +0000 (06:01 -0700)
(cherry picked from commit f6fd8aac13714ce17650eb4a648d5c08f0be53b4)

Co-authored-by: Batuhan Taskaya <isidentical@gmail.com>
Lib/ast.py
Lib/test/test_unparse.py
Misc/NEWS.d/next/Library/2022-05-11-19-33-27.gh-issue-92671.KE4v6a.rst [new file with mode: 0644]

index e81e28044bc6e47be1ed1917e2172b6fbb7762f5..4e2ae859245f99b76f70a3108bc1c6c39091f761 100644 (file)
@@ -1335,7 +1335,11 @@ class _Unparser(NodeVisitor):
             )
 
     def visit_Tuple(self, node):
-        with self.require_parens(_Precedence.TUPLE, node):
+        with self.delimit_if(
+            "(",
+            ")",
+            len(node.elts) == 0 or self.get_precedence(node) > _Precedence.TUPLE
+        ):
             self.items_view(self.traverse, node.elts)
 
     unop = {"Invert": "~", "Not": "not", "UAdd": "+", "USub": "-"}
index f999ae8c16ceafc0de29a855110d59d5deb9adc3..969aa16678f493552789d9c6f64ca6c3f2824ed2 100644 (file)
@@ -648,6 +648,9 @@ class CosmeticTestCase(ASTTestCase):
                     self.check_src_roundtrip(source.format(target=target))
 
     def test_star_expr_assign_target_multiple(self):
+        self.check_src_roundtrip("() = []")
+        self.check_src_roundtrip("[] = ()")
+        self.check_src_roundtrip("() = [a] = c, = [d] = e, f = () = g = h")
         self.check_src_roundtrip("a = b = c = d")
         self.check_src_roundtrip("a, b = c, d = e, f = g")
         self.check_src_roundtrip("[a, b] = [c, d] = [e, f] = g")
diff --git a/Misc/NEWS.d/next/Library/2022-05-11-19-33-27.gh-issue-92671.KE4v6a.rst b/Misc/NEWS.d/next/Library/2022-05-11-19-33-27.gh-issue-92671.KE4v6a.rst
new file mode 100644 (file)
index 0000000..b50677a
--- /dev/null
@@ -0,0 +1 @@
+Fixed :func:`ast.unparse` for empty tuples in the assignment target context.