Skip to content

Commit

Permalink
Try to simplify parens handling
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Oct 22, 2024
1 parent 0872111 commit 2f1689a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
38 changes: 13 additions & 25 deletions rewrite/rewrite/python/_parser_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1709,19 +1709,6 @@ def visit_Tuple(self, node):
if maybe_parens:
self._cursor += 1
omit_parens = False
expr_prefix = self.__whitespace()
self._parentheses_stack.append(
(lambda c, r: cast(JContainer, c)
.with_elements(
[e.with_prefix(expr_prefix) if i == 0 else e for i, e in enumerate(c.elements)])
if isinstance(c, JContainer) else j.Parentheses(
random_id(),
Space.EMPTY,
Markers.EMPTY,
self.__pad_right(c.with_prefix(expr_prefix), r)
),
self._cursor, node, Space.EMPTY)
)
else:
self._cursor = save_cursor
omit_parens = True
Expand All @@ -1736,7 +1723,7 @@ def visit_Tuple(self, node):
Markers.EMPTY
)

if len(self._parentheses_stack) > 0 and self._parentheses_stack[-1][2] == node and self.__cursor_at(')'):
if len(self._parentheses_stack) > 0 and self.__cursor_at(')'):
self._cursor += 1
elements = self._parentheses_stack.pop()[0](elements, Space.EMPTY)
omit_parens = False
Expand Down Expand Up @@ -1848,7 +1835,7 @@ def __convert_type(self, node) -> Optional[j.TypeTree]:

def __convert_internal(self, node, recursion) -> Optional[J]:
if node:
if isinstance(node, ast.expr) and not isinstance(node, (ast.Tuple, ast.GeneratorExp)):
if isinstance(node, ast.expr) and not isinstance(node, (ast.GeneratorExp)):
save_cursor = self._cursor

# noinspection PyUnusedLocal
Expand All @@ -1858,12 +1845,17 @@ def __convert_internal(self, node, recursion) -> Optional[J]:
if self._cursor < len(self._source) and self._source[self._cursor] == '(':
self._cursor += 1
expr_prefix = self.__whitespace()
self._parentheses_stack.append((lambda e, r: j.Parentheses(
random_id(),
prefix,
Markers.EMPTY,
self.__pad_right(e.with_prefix(expr_prefix), r)
), self._cursor, node, prefix))
self._parentheses_stack.append(
(lambda e, r: cast(JContainer, e)
.with_before(prefix)
.with_elements(
[e.with_prefix(expr_prefix) if i == 0 else e for i, e in enumerate(e.elements)])
if isinstance(e, JContainer) else j.Parentheses(
random_id(),
prefix,
Markers.EMPTY,
self.__pad_right(e.with_prefix(expr_prefix), r)
), self._cursor, node, prefix))
# handle nested parens
result = recursion(node)
else:
Expand All @@ -1887,10 +1879,6 @@ def __convert_internal(self, node, recursion) -> Optional[J]:
self._cursor = save_cursor_2
return result
else:
if isinstance(node, ast.expr) and not self.__cursor_at('(') and len(self._parentheses_stack) > 0 and self._parentheses_stack[-1][1] == self._cursor:
popped = self._parentheses_stack.pop()
self._cursor -= 1
return cast(J, self.visit(cast(ast.AST, node))).with_prefix(popped[3])
return self.visit(cast(ast.AST, node))
else:
return None
Expand Down
13 changes: 13 additions & 0 deletions rewrite/tests/python/all/binary_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,16 @@ def test_chained_comparison():
def test_chained_comparison_2():
# language=python
rewrite_run(python("assert 1 is not 2 is not 3"))


def test_multiline_tuple_comparison():
# language=python
rewrite_run(
python(
"""\
(
True
, False) is not None
""",
),
)

0 comments on commit 2f1689a

Please sign in to comment.