tawazi
tawazi copied to clipboard
[FEATURE] raise an error when to_dag decorates a function that invokes non ExecNode code
here is the key to the solution proposed by chatgpt:
In Python, you can use the ast module to parse the source code of a function and inspect its Abstract Syntax Tree (AST). You can then traverse the AST to find all function call expressions and extract the names of the functions being called. Here's an example:
import ast
def get_called_functions(func):
tree = ast.parse(func.__code__.co_code)
called_functions = []
for node in ast.walk(tree):
if isinstance(node, ast.Call):
called_functions.append(node.func.id)
return called_functions
def foo():
bar()
baz()
print(get_called_functions(foo)) # prints ['bar', 'baz']
This example uses the ast.parse() function to parse the bytecode of the foo function and create an AST. It then uses the ast.walk() function to traverse the AST and find all nodes of type ast.Call, which represent function calls. The node.func.id attribute returns the name of the function being called.
Keep in mind that this example only works for functions defined with the def statement, and will not work for functions defined using lambda or other statements.
Is this really relevant ?
yes because it eliminates confusion... but I don't think this is possible actually!