Skip to content

Commit ddbcf92

Browse files
committed
Fix higher order function call parsing
1 parent 6cfbcda commit ddbcf92

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

pythonwhat/parsing.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import ast
2+
import re
3+
24
from pythonwhat.utils_ast import wrap_in_module
35
from collections.abc import Sequence, Mapping
46
from collections import OrderedDict
@@ -326,13 +328,15 @@ def visit_Dict(self, node):
326328
def visit_Call(self, node):
327329
if self.call_lookup_active:
328330
self.visit(node.func)
331+
self.gen_name += "()"
329332
else:
330333
self.call_lookup_active = True
331334
self.visit(
332335
node.func
333336
) # Need to visit func to start recording the current function name.
334337

335338
if self.gen_name:
339+
self.gen_name = re.sub(r"(?:\(\))+(.)", "\\1", self.gen_name)
336340
if self.gen_name not in self.out:
337341
self.out[self.gen_name] = []
338342

tests/test_check_function.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -522,18 +522,26 @@ def test_function_call_in_comparison(code):
522522
assert res["correct"]
523523

524524

525-
def test_ho_function():
526-
# TODO: FunctionParser.visit_Call should append something to name to discern HOF calls
527-
# e.g. () if node.func is Func (this should only affect limited exercises)
528-
sct = "Ex().check_function('hof').check_args(0).has_equal_value(override=2)"
525+
@pytest.mark.parametrize(
526+
"sct",
527+
[
528+
"Ex().check_function('numpy.array')",
529+
"Ex().check_function('hof').check_args(0).has_equal_value(override=1)",
530+
"Ex().check_function('hof()').check_args(0).has_equal_value(override=2)",
531+
],
532+
)
533+
def test_ho_function(sct):
529534

530535
code = """
536+
import numpy as np
537+
np.array([])
538+
531539
def hof(arg1):
532540
def inner(arg2):
533541
return arg1, arg2
534-
542+
535543
return inner
536-
544+
537545
hof(1)(2)
538546
"""
539547

0 commit comments

Comments
 (0)