mindee / tawazi

A DAG Scheduler library written in pure python
https://mindee.github.io/tawazi/
Apache License 2.0
81 stars 5 forks source link

[FEATURE] raise an error when to_dag decorates a function that invokes non ExecNode code #53

Open bashirmindee opened 1 year ago

bashirmindee commented 1 year ago

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.

matthiasmindee commented 1 year ago

Is this really relevant ?

bashirmindee commented 1 year ago

yes because it eliminates confusion... but I don't think this is possible actually!