Skip to content

Commit

Permalink
Small corrections to array slice parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Nov 14, 2024
1 parent 2710b03 commit 3ddde7a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
24 changes: 16 additions & 8 deletions rewrite/rewrite/python/_parser_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1743,14 +1743,22 @@ def visit_SetComp(self, node):

def visit_Slice(self, node):
prefix = self.__whitespace()
if node.lower:
lower = self.__pad_right(self.__convert(node.lower), self.__source_before(':'))
else:
lower = self.__pad_right(j.Empty(random_id(), Space.EMPTY, Markers.EMPTY), self.__source_before(':'))
upper = self.__pad_right(
self.__convert(node.upper) if node.upper else j.Empty(random_id(), Space.EMPTY, Markers.EMPTY),
self.__source_before(':') if node.step else self.__whitespace('\n'))
step = self.__pad_right(self.__convert(node.step), self.__whitespace('\n')) if node.step else None

lower_expr = self.__convert(node.lower) if node.lower else None
right_padding = self.__whitespace(':')
lower = self.__pad_right(lower_expr, right_padding) if lower_expr else None
self._cursor += 1

upper_expr = self.__convert(node.upper) if node.upper else None
right_padding = self.__whitespace()
has_step = self.__cursor_at(':')
upper = self.__pad_right(upper_expr if node.upper else j.Empty(random_id(), self.__whitespace(), Markers.EMPTY), right_padding)
self._cursor += 1

step = self.__pad_right(
self.__convert(node.step) if node.step else j.Empty(random_id(), Space.EMPTY, Markers.EMPTY),
self.__whitespace(']')) if node.step or has_step else None

return py.Slice(
random_id(),
prefix,
Expand Down
10 changes: 10 additions & 0 deletions rewrite/tests/python/all/array_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ def test_array_slice_no_upper():
rewrite_run(python("a = [1, 2][0:]"))


def test_array_slice_all_empty():
# language=python
rewrite_run(python("a = [1, 2][ : : ]"))


def test_array_slice_empty_upper_and_step():
# language=python
rewrite_run(python("a = [1, 2][0::]"))


def test_array_slice_no_lower():
# language=python
rewrite_run(python("a = [1, 2][:1]"))
Expand Down

0 comments on commit 3ddde7a

Please sign in to comment.