8000 gh-92671: Don't omit parentheses when unparsing empty tuples (GH-92673) · python/cpython@52e6596 · GitHub
[go: up one dir, main page]

Skip to content

Commit 52e6596

Browse files
gh-92671: Don't omit parentheses when unparsing empty tuples (GH-92673)
(cherry picked from commit f6fd8aa) Co-authored-by: Batuhan Taskaya <isidentical@gmail.com>
1 parent 2ef6a98 commit 52e6596

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

Lib/ast.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1335,7 +1335,11 @@ def write_item(item):
13351335
)
13361336

13371337
def visit_Tuple(self, node):
1338-
with self.require_parens(_Precedence.TUPLE, node):
1338+
with self.delimit_if(
1339+
"(",
1340+
")",
1341+
len(node.elts) == 0 or self.get_precedence(node) > _Precedence.TUPLE
1342+
):
13391343
self.items_view(self.traverse, node.elts)
13401344

13411345
unop = {"Invert": "~", "Not": "not", "UAdd": "+", "USub": "-"}

Lib/test/test_unparse.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,9 @@ def test_star_expr_assign_target(self):
648648
self.check_src_roundtrip(source.format(target=target))
649649

650650
def test_star_expr_assign_target_multiple(self):
651+
self.check_src_roundtrip("() = []")
652+
self.check_src_roundtrip("[] = ()")
653+
self.check_src_roundtrip("() = [a] = c, = [d] = e, f = () = g = h")
651654
self.check_src_roundtrip("a = b = c = d")
652655
self.check_src_roundtrip("a, b = c, d = e, f = g")
653656
self.check_src_roundtrip("[a, b] = [c, d] = [e, f] = g")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed :func:`ast.unparse` for empty tuples in the assignment target context.

0 commit comments

Comments
 (0)
0