[Python-checkins] gh-92671: Don't omit parentheses when unparsing empty tuples (GH-92673)

miss-islington webhook-mailer at python.org
Mon May 16 09:01:51 EDT 2022


https://github.com/python/cpython/commit/52e6596fb5f9371f3a1434dd7816e400862b4df8
commit: 52e6596fb5f9371f3a1434dd7816e400862b4df8
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-05-16T06:01:34-07:00
summary:

gh-92671: Don't omit parentheses when unparsing empty tuples (GH-92673)

(cherry picked from commit f6fd8aac13714ce17650eb4a648d5c08f0be53b4)

Co-authored-by: Batuhan Taskaya <isidentical at gmail.com>

files:
A Misc/NEWS.d/next/Library/2022-05-11-19-33-27.gh-issue-92671.KE4v6a.rst
M Lib/ast.py
M Lib/test/test_unparse.py

diff --git a/Lib/ast.py b/Lib/ast.py
index e81e28044bc6e..4e2ae859245f9 100644
--- a/Lib/ast.py
+++ b/Lib/ast.py
@@ -1335,7 +1335,11 @@ def write_item(item):
             )
 
     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": "-"}
diff --git a/Lib/test/test_unparse.py b/Lib/test/test_unparse.py
index f999ae8c16cea..969aa16678f49 100644
--- a/Lib/test/test_unparse.py
+++ b/Lib/test/test_unparse.py
@@ -648,6 +648,9 @@ def test_star_expr_assign_target(self):
                     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
index 0000000000000..b50677ab5ca10
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-05-11-19-33-27.gh-issue-92671.KE4v6a.rst
@@ -0,0 +1 @@
+Fixed :func:`ast.unparse` for empty tuples in the assignment target context.



More information about the Python-checkins mailing list