Skip to content

CallGraphNode

Class for call graph nodes.

A call graph node represents a function in the call graph.

Attributes:

Name Type Description
symbol Symbol

The symbol of the function that the node represents.

reasons Reasons

The raw Reasons for the node. After the call graph is built, this only contains reads_from and writes_to as well as unknown_calls.

children dict[NodeID, CallGraphNode]

The set of children of the node, (i.e., the set of nodes that this node calls)

Source code in src/library_analyzer/processing/api/purity_analysis/model/_call_graph.py
@dataclass
class CallGraphNode:
    """Class for call graph nodes.

    A call graph node represents a function in the call graph.

    Attributes
    ----------
    symbol : Symbol
        The symbol of the function that the node represents.
    reasons : Reasons
        The raw Reasons for the node.
        After the call graph is built, this only contains reads_from and writes_to as well as unknown_calls.
    children : dict[NodeID, CallGraphNode]
        The set of children of the node, (i.e., the set of nodes that this node calls)
    """

    symbol: Symbol
    reasons: Reasons
    children: dict[NodeID, CallGraphNode] = field(default_factory=dict)

    def __hash__(self) -> int:
        return hash(str(self))

    def __str__(self) -> str:
        return f"{self.symbol.id}"

    def __repr__(self) -> str:
        return f"{self.symbol.name}: {id(self)}"

    def add_child(self, child: CallGraphNode) -> None:
        """Add a child to the node.

        Parameters
        ----------
        child : CallGraphNode
            The child to add.
        """
        self.children[child.symbol.id] = child

    def get_child(self, child_id: NodeID) -> CallGraphNode:
        """Get a child from the node.

        Parameters
        ----------
        child_id : NodeID
            The NodeID of the child to get.

        Raises
        ------
        KeyError
            If the child_id is not in the children.
        """
        result = self.children.get(child_id)
        if result is None:
            raise KeyError(f"Child with id {child_id} not found inside the call graph.")
        return result

    def has_child(self, child_id: NodeID) -> bool:
        """Check if the node has a child with the given NodeID.

        Parameters
        ----------
        child_id : NodeID
            The NodeID of the child to check for.

        Returns
        -------
        bool
            True if the node has a child with the given NodeID, False otherwise.
        """
        return child_id in self.children

    def delete_child(self, child_id: NodeID) -> None:
        """Delete a child from the node.

        Parameters
        ----------
        child_id : NodeID
            The NodeID of the child to delete.
        """
        del self.children[child_id]

    def is_leaf(self) -> bool:
        """Check if the node is a leaf node.

        Returns
        -------
        bool
            True if the node is a leaf node, False otherwise.
        """
        return len(self.children) == 0

    def is_inferred(self) -> bool:
        """Check if the result is already inferred."""
        return self.reasons.result is not None

children: dict[NodeID, CallGraphNode] = field(default_factory=dict) class-attribute instance-attribute

reasons: Reasons instance-attribute

symbol: Symbol instance-attribute

__hash__()

Source code in src/library_analyzer/processing/api/purity_analysis/model/_call_graph.py
def __hash__(self) -> int:
    return hash(str(self))

__init__(symbol, reasons, children=dict())

__repr__()

Source code in src/library_analyzer/processing/api/purity_analysis/model/_call_graph.py
def __repr__(self) -> str:
    return f"{self.symbol.name}: {id(self)}"

__str__()

Source code in src/library_analyzer/processing/api/purity_analysis/model/_call_graph.py
def __str__(self) -> str:
    return f"{self.symbol.id}"

add_child(child)

Add a child to the node.

Parameters:

Name Type Description Default
child CallGraphNode

The child to add.

required
Source code in src/library_analyzer/processing/api/purity_analysis/model/_call_graph.py
def add_child(self, child: CallGraphNode) -> None:
    """Add a child to the node.

    Parameters
    ----------
    child : CallGraphNode
        The child to add.
    """
    self.children[child.symbol.id] = child

delete_child(child_id)

Delete a child from the node.

Parameters:

Name Type Description Default
child_id NodeID

The NodeID of the child to delete.

required
Source code in src/library_analyzer/processing/api/purity_analysis/model/_call_graph.py
def delete_child(self, child_id: NodeID) -> None:
    """Delete a child from the node.

    Parameters
    ----------
    child_id : NodeID
        The NodeID of the child to delete.
    """
    del self.children[child_id]

get_child(child_id)

Get a child from the node.

Parameters:

Name Type Description Default
child_id NodeID

The NodeID of the child to get.

required

Raises:

Type Description
KeyError

If the child_id is not in the children.

Source code in src/library_analyzer/processing/api/purity_analysis/model/_call_graph.py
def get_child(self, child_id: NodeID) -> CallGraphNode:
    """Get a child from the node.

    Parameters
    ----------
    child_id : NodeID
        The NodeID of the child to get.

    Raises
    ------
    KeyError
        If the child_id is not in the children.
    """
    result = self.children.get(child_id)
    if result is None:
        raise KeyError(f"Child with id {child_id} not found inside the call graph.")
    return result

has_child(child_id)

Check if the node has a child with the given NodeID.

Parameters:

Name Type Description Default
child_id NodeID

The NodeID of the child to check for.

required

Returns:

Type Description
bool

True if the node has a child with the given NodeID, False otherwise.

Source code in src/library_analyzer/processing/api/purity_analysis/model/_call_graph.py
def has_child(self, child_id: NodeID) -> bool:
    """Check if the node has a child with the given NodeID.

    Parameters
    ----------
    child_id : NodeID
        The NodeID of the child to check for.

    Returns
    -------
    bool
        True if the node has a child with the given NodeID, False otherwise.
    """
    return child_id in self.children

is_inferred()

Check if the result is already inferred.

Source code in src/library_analyzer/processing/api/purity_analysis/model/_call_graph.py
def is_inferred(self) -> bool:
    """Check if the result is already inferred."""
    return self.reasons.result is not None

is_leaf()

Check if the node is a leaf node.

Returns:

Type Description
bool

True if the node is a leaf node, False otherwise.

Source code in src/library_analyzer/processing/api/purity_analysis/model/_call_graph.py
def is_leaf(self) -> bool:
    """Check if the node is a leaf node.

    Returns
    -------
    bool
        True if the node is a leaf node, False otherwise.
    """
    return len(self.children) == 0