mirror of
https://github.com/python/cpython.git
synced 2025-08-29 13:15:11 +00:00
bpo-39889: Fix ast.unparse() for subscript. (GH-18824)
This commit is contained in:
parent
31350f9af0
commit
c4928fc1a8
3 changed files with 34 additions and 2 deletions
19
Lib/ast.py
19
Lib/ast.py
|
@ -1261,7 +1261,17 @@ class _Unparser(NodeVisitor):
|
|||
self.set_precedence(_Precedence.ATOM, node.value)
|
||||
self.traverse(node.value)
|
||||
with self.delimit("[", "]"):
|
||||
self.traverse(node.slice)
|
||||
if (isinstance(node.slice, Index)
|
||||
and isinstance(node.slice.value, Tuple)
|
||||
and node.slice.value.elts):
|
||||
if len(node.slice.value.elts) == 1:
|
||||
elt = node.slice.value.elts[0]
|
||||
self.traverse(elt)
|
||||
self.write(",")
|
||||
else:
|
||||
self.interleave(lambda: self.write(", "), self.traverse, node.slice.value.elts)
|
||||
else:
|
||||
self.traverse(node.slice)
|
||||
|
||||
def visit_Starred(self, node):
|
||||
self.write("*")
|
||||
|
@ -1286,7 +1296,12 @@ class _Unparser(NodeVisitor):
|
|||
self.traverse(node.step)
|
||||
|
||||
def visit_ExtSlice(self, node):
|
||||
self.interleave(lambda: self.write(", "), self.traverse, node.dims)
|
||||
if len(node.dims) == 1:
|
||||
elt = node.dims[0]
|
||||
self.traverse(elt)
|
||||
self.write(",")
|
||||
else:
|
||||
self.interleave(lambda: self.write(", "), self.traverse, node.dims)
|
||||
|
||||
def visit_arg(self, node):
|
||||
self.write(node.arg)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue