Skip to content

Error running Callgraph Example from Docs #202

@JohnPeng47

Description

@JohnPeng47

Hi!

Getting an error running the callgraph example from the docs (https://docs.codegen.com/building-with-codegen/traversing-the-call-graph) on https://github.com/JohnPeng47/cowboy-old.


import networkx as nx
from codegen.sdk.core.interfaces.callable import FunctionCallDefinition
from codegen.sdk.core.function import Function
from codegen.sdk.core.codebase import Codebase

def create_call_graph(start_func, end_func, max_depth=5):
    G = nx.DiGraph()

    def traverse_calls(parent_func, current_depth):
        if current_depth > max_depth:
            return

        # Determine source node
        if isinstance(parent_func, Function):
            src_call = src_func = parent_func
        else:
            src_func = parent_func.function_definition
            src_call = parent_func

        print(f"Func[{current_depth}]: {src_func.name}")

        # Traverse all function calls
        for call in src_func.function_calls:
            func = call.function_definition

            # Error if line not added
            # Seems to be missing definitions for python libs?
            if not func:
                continue

            # Skip recursive calls
            if func.name == src_func.name:
                continue

            # Add nodes and edges
            G.add_node(call)
            G.add_edge(src_call, call)

            # Check if we reached the target
            if func == end_func:
                G.add_edge(call, end_func)
                return

            # Continue traversal
            traverse_calls(call, current_depth + 1)

    # Initialize graph
    G.add_node(start_func, color="blue")  # Start node
    G.add_node(end_func, color="red")     # End node

    # Start traversal
    traverse_calls(start_func, 1)
    return G


codebase = Codebase(".")

# Usage example
py_class = codebase.get_class("Composer")
start = py_class.get_method("generate_test")
py_class2 = codebase.get_class("AugmentAdditiveEvaluator")
end = py_class2.get_method("process_test_results")

graph = create_call_graph(start, end)

Error:

Traceback (most recent call last):
  File "/home/ubuntu/cowboy-server/test_codegen.py", line 70, in <module>
    graph = create_call_graph(start, end)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/cowboy-server/test_codegen.py", line 56, in create_call_graph
    traverse_calls(start_func, 1)
  File "/home/ubuntu/cowboy-server/test_codegen.py", line 49, in traverse_calls
    traverse_calls(call, current_depth + 1)
  File "/home/ubuntu/cowboy-server/test_codegen.py", line 49, in traverse_calls
    traverse_calls(call, current_depth + 1)
  File "/home/ubuntu/cowboy-server/test_codegen.py", line 49, in traverse_calls
    traverse_calls(call, current_depth + 1)
  [Previous line repeated 1 more time]
  File "/home/ubuntu/cowboy-server/test_codegen.py", line 21, in traverse_calls
    if not src_func.function_calls:
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/cowboy-server/.venv/lib/python3.12/site-packages/codegen/sdk/core/interfaces/editable.py", line 1044, in function_calls
    for node in self.children:
                ^^^^^^^^^^^^^
  File "/home/ubuntu/cowboy-server/.venv/lib/python3.12/site-packages/codegen/sdk/core/interfaces/editable.py", line 316, in children
    return [self._parse_expression(child) for child in self.ts_node.named_children]
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/cowboy-server/.venv/lib/python3.12/site-packages/codegen/sdk/core/interfaces/editable.py", line 917, in _parse_expression
    return self.G.parser.parse_expression(node, self.file_node_id, self.G, self, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/cowboy-server/.venv/lib/python3.12/site-packages/codegen/sdk/core/parser.py", line 70, in parse_expression
    if previous := parent.file._range_index.get_canonical_for_range(node.range, node.kind_id):
                   ^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute '_range_index'

Also, as noted in the code, if the line is commented out, then another error occurs

            # Error if line not added
            # Seems to be missing definitions for python libs?
            if not func:
                continue

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions