summaryrefslogtreecommitdiffstats
path: root/Lib/ast.py
diff options
context:
space:
mode:
authorBatuhan Taskaya <batuhanosmantaskaya@gmail.com>2020-05-18 20:48:49 (GMT)
committerGitHub <noreply@github.com>2020-05-18 20:48:49 (GMT)
commitc102a148256b00b7d48c51a1a97df19042e603de (patch)
tree7bc9b18b124fdcceeb28ce56eff2fec81e2188ed /Lib/ast.py
parent75b863aa97016c6813709eb620c43295f84dd51f (diff)
downloadcpython-c102a148256b00b7d48c51a1a97df19042e603de.zip
cpython-c102a148256b00b7d48c51a1a97df19042e603de.tar.gz
cpython-c102a148256b00b7d48c51a1a97df19042e603de.tar.bz2
bpo-38870: Don't omit parenthesis when unparsing a slice in ast.unparse
When unparsing a non-empty tuple, the parentheses can be safely omitted if there aren't any elements that explicitly require them (such as starred expressions).
Diffstat (limited to 'Lib/ast.py')
-rw-r--r--Lib/ast.py12
1 files changed, 11 insertions, 1 deletions
diff --git a/Lib/ast.py b/Lib/ast.py
index 2edb717..52e51b4 100644
--- a/Lib/ast.py
+++ b/Lib/ast.py
@@ -1356,10 +1356,20 @@ class _Unparser(NodeVisitor):
self.traverse(e)
def visit_Subscript(self, node):
+ def is_simple_tuple(slice_value):
+ # when unparsing a non-empty tuple, the parantheses can be safely
+ # omitted if there aren't any elements that explicitly requires
+ # parantheses (such as starred expressions).
+ return (
+ isinstance(slice_value, Tuple)
+ and slice_value.elts
+ and not any(isinstance(elt, Starred) for elt in slice_value.elts)
+ )
+
self.set_precedence(_Precedence.ATOM, node.value)
self.traverse(node.value)
with self.delimit("[", "]"):
- if isinstance(node.slice, Tuple) and node.slice.elts:
+ if is_simple_tuple(node.slice):
self.items_view(self.traverse, node.slice.elts)
else:
self.traverse(node.slice)