Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/astunparse/unparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ def leave(self):
"Decrease the indentation level."
self._indent -= 1

def dispatch(self, tree):
def dispatch(self, tree, **kw):
"Dispatcher function, dispatching tree type T to method _T."
if isinstance(tree, list):
for t in tree:
self.dispatch(t)
return
meth = getattr(self, "_"+tree.__class__.__name__)
meth(tree)
meth(tree, **(kw if meth.__name__ in ["_Tuple"] else {}))


############### Unparsing methods ######################
Expand Down Expand Up @@ -648,15 +648,15 @@ def write_item(item):
interleave(lambda: self.write(", "), write_item, zip(t.keys, t.values))
self.write("}")

def _Tuple(self, t):
self.write("(")
def _Tuple(self, t, noparen=False, **kw):
if not noparen: self.write("(")
if len(t.elts) == 1:
elt = t.elts[0]
self.dispatch(elt)
self.write(",")
else:
interleave(lambda: self.write(", "), self.dispatch, t.elts)
self.write(")")
if not noparen: self.write(")")

unop = {"Invert":"~", "Not": "not", "UAdd":"+", "USub":"-"}
def _UnaryOp(self, t):
Expand Down Expand Up @@ -741,7 +741,7 @@ def _Call(self, t):
def _Subscript(self, t):
self.dispatch(t.value)
self.write("[")
self.dispatch(t.slice)
self.dispatch(t.slice, noparen=True)
self.write("]")

def _Starred(self, t):
Expand Down
12 changes: 12 additions & 0 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,18 @@ def test_raise_from(self):
def test_bytes(self):
self.check_roundtrip("b'123'")

def test_index(self):
self.check_roundtrip("r[0]")
self.check_roundtrip("r[0:5]")
self.check_roundtrip("r[0:5:2]")
self.check_roundtrip("r[1::2]")

def test_index2(self):
self.check_roundtrip("r[0,i]")
self.check_roundtrip("r[:,0:5]")
self.check_roundtrip("r[i:1:-1, 2]")
self.check_roundtrip("r[i:1:-1, :]")

@unittest.skipIf(sys.version_info < (3, 6), "Not supported < 3.6")
def test_formatted_value(self):
self.check_roundtrip('f"{value}"')
Expand Down