[BUGFIX] Custom function ordering (#1104)

Fixes a bug where custom functions would throw an error if they were used out-of-order from their definition
This commit is contained in:
Jesse Bannon 2024-10-26 21:16:17 -07:00 committed by GitHub
parent 43c10c19e0
commit 1bdc65f2e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 4 deletions

View file

@ -490,15 +490,18 @@ class Script:
name: definition for name, definition in variables.items() if not _is_function(name)
}
custom_function_names = set(self._functions.keys()) | functions_to_add.keys()
variable_names = (
set(self._variables.keys()) | variables_to_add.keys() | (unresolvable or set())
)
for definitions in [functions_to_add, variables_to_add]:
for name, definition in definitions.items():
parsed = parse(
text=definition,
name=name,
custom_function_names=set(self._functions.keys()),
variable_names=set(self._variables.keys())
.union(variables.keys())
.union(unresolvable or set()),
custom_function_names=custom_function_names,
variable_names=variable_names,
)
if parsed.maybe_resolvable is None:

View file

@ -22,6 +22,24 @@ class TestCustomFunction:
}
).resolve() == ScriptOutput({"output": Integer(9)})
def test_custom_functions_any_order_via_add(self):
assert Script({}).add(
{
"%custom_cubed": "{%mul(%custom_square($0),$0)}",
"%custom_square": "{%mul($0, $0)}",
"output": "{%custom_cubed(3)}",
}
).resolve() == ScriptOutput({"output": Integer(27)})
def test_custom_functions_any_order_via_init(self):
assert Script(
{
"%custom_cubed": "{%mul(%custom_square($0),$0)}",
"%custom_square": "{%mul($0, $0)}",
"output": "{%custom_cubed(3)}",
}
).resolve() == ScriptOutput({"output": Integer(27)})
def test_custom_function_cycle(self):
with pytest.raises(
CycleDetected, match=re.escape("The custom function %cycle_func cannot call itself.")