Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
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
14 changes: 14 additions & 0 deletions 14 Lib/test/test_tools/test_unparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,20 @@ def test_dict_unpacking_in_dict(self):
self.check_roundtrip(r"""{**{'y': 2}, 'x': 1}""")
self.check_roundtrip(r"""{**{'y': 2}, **{'x': 1}}""")

def test_subscript(self):
self.check_roundtrip("a[i]")
self.check_roundtrip("a[i,]")
self.check_roundtrip("a[i, j]")
self.check_roundtrip("a[()]")
self.check_roundtrip("a[i:j]")
self.check_roundtrip("a[:j]")
self.check_roundtrip("a[i:]")
self.check_roundtrip("a[i:j:k]")
self.check_roundtrip("a[:j:k]")
self.check_roundtrip("a[i::k]")
self.check_roundtrip("a[i:j,]")
self.check_roundtrip("a[i:j, k]")


class DirectoryTestCase(ASTTestCase):
"""Test roundtrip behaviour on all files in Lib and Lib/test."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed ``unparse.py`` for extended slices containing a single element (e.g.
``a[i:j,]``). Remove redundant tuples when index with a tuple (e.g. ``a[i,
j]``).
19 changes: 17 additions & 2 deletions 19 Tools/parser/unparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,17 @@ def _Call(self, t):
def _Subscript(self, t):
self.dispatch(t.value)
self.write("[")
self.dispatch(t.slice)
if (isinstance(t.slice, ast.Index)
and isinstance(t.slice.value, ast.Tuple)
and t.slice.value.elts):
if len(t.slice.value.elts) == 1:
elt = t.slice.value.elts[0]
self.dispatch(elt)
self.write(",")
else:
interleave(lambda: self.write(", "), self.dispatch, t.slice.value.elts)
else:
self.dispatch(t.slice)
self.write("]")

def _Starred(self, t):
Expand All @@ -581,7 +591,12 @@ def _Slice(self, t):
self.dispatch(t.step)

def _ExtSlice(self, t):
interleave(lambda: self.write(', '), self.dispatch, t.dims)
if len(t.dims) == 1:
elt = t.dims[0]
self.dispatch(elt)
self.write(",")
else:
interleave(lambda: self.write(', '), self.dispatch, t.dims)

# argument
def _arg(self, t):
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.