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

[BUGFIX] Custom function ordering #1104

Merged
merged 1 commit into from
Oct 27, 2024
Merged
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
11 changes: 7 additions & 4 deletions src/ytdl_sub/script/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,15 +490,18 @@ def add(self, variables: Dict[str, str], unresolvable: Optional[Set[str]] = None
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:
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/script/types/test_custom_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ def test_custom_function_use_input_param_multiple_times(self):
}
).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.")
Expand Down
Loading