diff --git a/src/ytdl_sub/script/types/syntax_tree.py b/src/ytdl_sub/script/types/syntax_tree.py index 18ac7579..b49bd6cf 100644 --- a/src/ytdl_sub/script/types/syntax_tree.py +++ b/src/ytdl_sub/script/types/syntax_tree.py @@ -45,40 +45,30 @@ class SyntaxTree(VariableDependency): return String("".join([str(res) for res in resolved])) @classmethod - def _get_custom_function_dependencies( + def _traverse_custom_function_dependencies( cls, custom_function_name: str, custom_function_dependency: "SyntaxTree", custom_functions: Dict[str, "SyntaxTree"], deps: List[str], - ) -> List[str]: - deps = copy.deepcopy(deps) # do not work with references - + ) -> None: for dep in custom_function_dependency.custom_functions: - # Skip leaf functions since they will never cause a cycle - if not custom_functions[dep.name].custom_functions: - continue - - deps.append(dep.name) - - if custom_function_name in deps: - cycle_deps = [custom_function_name] + deps[0 : deps.index(custom_function_name) + 1] + if custom_function_name in deps + [dep.name]: + cycle_deps = [custom_function_name] + deps + [dep.name] cycle_deps_str = " -> ".join([f"%{name}" for name in cycle_deps]) raise CycleDetected(f"Custom functions contain a cycle: {cycle_deps_str}") - deps += cls._get_custom_function_dependencies( + cls._traverse_custom_function_dependencies( custom_function_name=custom_function_name, custom_function_dependency=custom_functions[dep.name], custom_functions=custom_functions, - deps=deps, + deps=deps + [dep.name], ) - return deps - @classmethod def _ensure_no_custom_function_cycles(cls, custom_functions: Dict[str, "SyntaxTree"]): for custom_function_name, custom_function in custom_functions.items(): - _ = cls._get_custom_function_dependencies( + cls._traverse_custom_function_dependencies( custom_function_name=custom_function_name, custom_function_dependency=custom_function, custom_functions=custom_functions,