[BUGFIX] Fix custom function lambda variable dependency issue (#1402)

There was a bug that did not properly check variable dependency when using lambda functions. It could still work depending on order of definitions. This fix should make it work no matter the order.
This commit is contained in:
Jesse Bannon 2025-12-30 19:53:15 -08:00 committed by GitHub
parent 1abe2a44f5
commit d373935fff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 1 deletions

View file

@ -114,6 +114,7 @@ class VariableDependency(ABC):
output.add(ParsedCustomFunction(name=arg.name, num_input_args=len(arg.args)))
if isinstance(arg, VariableDependency):
output.update(arg.custom_functions)
# if isinstance(arg, Lambda)
return output
@ -189,7 +190,17 @@ class VariableDependency(ABC):
-------
True if it contains any of the input variables. False otherwise.
"""
for custom_function in self.custom_functions:
# If there are lambdas, see if they are custom functions. If so, check them
custom_functions_to_check = self.custom_functions
for lambda_func in self.lambdas:
if lambda_func.value in custom_function_definitions:
custom_functions_to_check.add(
ParsedCustomFunction(
name=lambda_func.value, num_input_args=lambda_func.num_input_args()
)
)
for custom_function in custom_functions_to_check:
if custom_function_definitions[custom_function.name].contains(
variables=variables, custom_function_definitions=custom_function_definitions
):

View file

@ -76,6 +76,28 @@ class TestLambdaFunction:
assert script.resolve().get("category_url_map").native == {1: 1, 2: 2, 3: 3}
def test_array_apply_custom_function(self):
output = (
Script(
{
"the_array": "{ ['a', 'B', 'c', 'D'] }",
"output": "{ %array_apply(the_array, %lower) }",
"should_lower": "{%bool(True)}",
"%custom_cap": """{
%if(
%bool(should_lower),
%lower($0),
%upper($0)
)
}""",
}
)
.resolve(update=True)
.get("output")
.native
)
assert output == ["a", "b", "c", "d"]
class TestLambdaFunctionIncompatibleNumArguments:
@pytest.mark.parametrize(