Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CX-1204] Fix: check higher order function arguments #391

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pythonwhat/parsing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import ast
import re

from pythonwhat.utils_ast import wrap_in_module
from collections.abc import Sequence, Mapping
from collections import OrderedDict
Expand Down Expand Up @@ -326,13 +328,15 @@ def visit_Dict(self, node):
def visit_Call(self, node):
if self.call_lookup_active:
self.visit(node.func)
self.gen_name += "()"
else:
self.call_lookup_active = True
self.visit(
node.func
) # Need to visit func to start recording the current function name.

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

Expand Down
27 changes: 27 additions & 0 deletions tests/test_check_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,3 +520,30 @@ def test_function_call_in_comparison(code):
sct = "Ex().check_function('len')"
res = helper.run({"DC_CODE": code, "DC_SOLUTION": code, "DC_SCT": sct})
assert res["correct"]


@pytest.mark.parametrize(
"sct",
[
"Ex().check_function('numpy.array')",
"Ex().check_function('hof').check_args(0).has_equal_value(override=1)",
"Ex().check_function('hof()').check_args(0).has_equal_value(override=2)",
],
)
def test_ho_function(sct):

code = """
import numpy as np
np.array([])

def hof(arg1):
def inner(arg2):
return arg1, arg2

return inner

hof(1)(2)
"""

res = helper.run({"DC_CODE": code, "DC_SOLUTION": code, "DC_SCT": sct})
assert res["correct"]