Skip to content

Commit

Permalink
Specifically ignore empty slices
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmojaki committed Oct 30, 2022
1 parent c30bd05 commit 5c7e360
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
4 changes: 3 additions & 1 deletion asttokens/mark_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ def _visit_after_children(self, node, parent_token, token):
last = None
for child in cast(Callable, self._iter_children)(node):
# astroid slices have especially wrong positions, we don't want them to corrupt their parents.
if not first or child.first_token.index < first.index and not util.is_astroid_slice(child):
if util.is_empty_astroid_slice(child):
continue
if not first or child.first_token.index < first.index:
first = child.first_token
if not last or child.last_token.index > last.index:
last = child.last_token
Expand Down
8 changes: 6 additions & 2 deletions asttokens/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,13 @@ def is_slice(node):
)


def is_astroid_slice(node):
def is_empty_astroid_slice(node):
# type: (AstNode) -> bool
return is_slice(node) and not isinstance(node, ast.AST)
return (
node.__class__.__name__ == "Slice"
and not isinstance(node, ast.AST)
and node.lower is node.upper is node.step
)


# Sentinel value used by visit_tree().
Expand Down
2 changes: 1 addition & 1 deletion tests/test_mark_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def test_deep_recursion(self):

def test_slices(self):
# Make sure we don't fail on parsing slices of the form `foo[4:]`.
source = "(foo.Area_Code, str(foo.Phone)[:3], str(foo.Phone)[3:], foo[:], bar[::2, :], [a[:]][::-1])"
source = "(foo.Area_Code, str(foo.Phone)[:3], str(foo.Phone)[3:], foo[:], bar[::2, :], bar2[:, ::2], [a[:]][::-1])"
m = self.create_mark_checker(source)
self.assertIn("Tuple:" + source, m.view_nodes_at(1, 0))
self.assertEqual(m.view_nodes_at(1, 1),
Expand Down

0 comments on commit 5c7e360

Please sign in to comment.