diff --git a/rewrite/rewrite/python/_parser_visitor.py b/rewrite/rewrite/python/_parser_visitor.py index d897acbe..120d1329 100644 --- a/rewrite/rewrite/python/_parser_visitor.py +++ b/rewrite/rewrite/python/_parser_visitor.py @@ -1745,19 +1745,21 @@ def visit_Slice(self, node): prefix = self.__whitespace() lower_expr = self.__convert(node.lower) if node.lower else None - right_padding = self.__whitespace(':') + right_padding = self.__whitespace(':') if node.lower else Space.EMPTY 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() + upper = self.__pad_right(upper_expr if node.upper else j.Empty(random_id(), Space.EMPTY, Markers.EMPTY), right_padding) 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 + if has_step: + 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 Space.EMPTY + else: + step = None return py.Slice( random_id(), diff --git a/rewrite/tests/python/all/array_test.py b/rewrite/tests/python/all/array_test.py index 17c68228..b8f5a18f 100644 --- a/rewrite/tests/python/all/array_test.py +++ b/rewrite/tests/python/all/array_test.py @@ -31,6 +31,17 @@ def test_array_slice_all_empty(): rewrite_run(python("a = [1, 2][ : : ]")) +def test_comment(): + # language=python + rewrite_run( + python(r''' + a = d[:0] + a = d[0:] + ''' + ) + ) + + def test_array_slice_empty_upper_and_step(): # language=python rewrite_run(python("a = [1, 2][0::]"))