Skip to content

Commit

Permalink
Allow array accesses as function call receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 6, 2024
1 parent 464c116 commit 7eb59c6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
10 changes: 5 additions & 5 deletions rewrite/rewrite/python/_parser_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1039,10 +1039,6 @@ def visit_Call(self, node):
if isinstance(node.func, ast.Name):
select = None
name = cast(j.Identifier, self.__convert(node.func))
elif isinstance(node.func, ast.Call):
select = self.__pad_right(cast(Expression, self.__convert(node.func)), self.__whitespace())
# printer handles empty name by not printing `.` before it
name = self.__convert_name('')
elif isinstance(node.func, ast.Attribute):
select = self.__pad_right(self.__convert(node.func.value), self.__source_before('.'))
name = j.Identifier(
Expand All @@ -1054,8 +1050,12 @@ def visit_Call(self, node):
self.__map_type(node.func.value),
None
)
elif isinstance(node.func, (ast.Call, ast.Subscript)):
select = self.__pad_right(cast(Expression, self.__convert(node.func)), self.__whitespace())
# printer handles empty name by not printing `.` before it
name = self.__convert_name('')
else:
raise NotImplementedError("Calls to functions other than methods are not yet supported")
raise NotImplementedError(f"Calls to functions other than methods are not yet supported: {type(node.func)}")

all_args = node.args + node.keywords
args = JContainer(
Expand Down
6 changes: 4 additions & 2 deletions rewrite/rewrite/python/parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ast
import traceback
import logging
from pathlib import Path
from typing import Iterable, Optional

Expand All @@ -8,6 +8,8 @@
from ._parser_visitor import ParserVisitor
from .tree import CompilationUnit

logging.basicConfig(level=logging.ERROR)


class PythonParser(Parser):
def parse_inputs(self, sources: Iterable[ParserInput], relative_to: Optional[Path],
Expand All @@ -20,7 +22,7 @@ def parse_inputs(self, sources: Iterable[ParserInput], relative_to: Optional[Pat
cu = ParserVisitor(source_str).visit(tree)
cu = require_print_equals_input(self, cu, source, relative_to, ctx)
except Exception as e:
traceback.print_exc()
logging.error(f"An error was encountered while parsing {source.path}: {str(e)}", exc_info=True)
cu = ParseError.build(self, source, relative_to, ctx, e)
yield cu

Expand Down
5 changes: 5 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,11 @@ def test_invoke_function_receiver():
rewrite_run(python("assert a(0)(1)"))


def test_invoke_array_access_receiver():
# language=python
rewrite_run(python("assert a[0](1)"))


def test_sequence_unpack():
# language=python
rewrite_run(
Expand Down

0 comments on commit 7eb59c6

Please sign in to comment.