Skip to content

Commit

Permalink
Support method invocations with receiver in parentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Nov 19, 2024
1 parent ec1ba4f commit 224fc07
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
17 changes: 17 additions & 0 deletions rewrite/rewrite/python/_parser_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,23 @@ def visit_Call(self, node):
self.__map_type(node.func.value),
None
)
save_cursor = self._cursor
parens_right_padding = self.__whitespace()
if self.__cursor_at(')'):
name = j.FieldAccess(
random_id(),
Space.EMPTY,
Markers.EMPTY,
select.element,
JLeftPadded(select.after, name, Markers.EMPTY),
None
)
while len(self._parentheses_stack) > 0 and self.__cursor_at(')'):
self._cursor += 1
name = self._parentheses_stack.pop()[0](name, parens_right_padding)
save_cursor = self._cursor
parens_right_padding = self.__whitespace()
self._cursor = save_cursor
else:
select = self.__pad_right(cast(Expression, self.__convert(node.func)), self.__whitespace())
# printer handles empty name by not printing `.` before it
Expand Down
15 changes: 15 additions & 0 deletions rewrite/tests/python/all/method_invocation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ def test_invoke_function_receiver():
rewrite_run(python("assert a(0)(1)"))


def test_qualified_receiver_with_parens():
# language=python
rewrite_run(
python(
"""
class Foo:
def getter(self, row):
pass
def foo(self):
return (self.getter )(1)
"""
)
)


def test_invoke_lambda_receiver():
# language=python
rewrite_run(python("assert (lambda x: x)(1)"))
Expand Down

0 comments on commit 224fc07

Please sign in to comment.