Skip to content

Reasons

Represents a function and the raw reasons for impurity.

Raw reasons means that the reasons are just collected and not yet processed.

Attributes:

Name Type Description
function_scope FunctionScope | None

The scope of the function which the reasons belong to. Is None if the reasons are not for a FunctionDef node. This is the case when either a builtin or a combined node is created, or a ClassScope is used to propagate reasons.

writes_to dict[NodeID, NonLocalVariableWrite]

A dict of all nodes that are written to.

reads_from dict[NodeID, NonLocalVariableRead]

A dict of all nodes that are read from.

calls set[Symbol]

A set of all nodes that are called.

result PurityResult | None

The result of the purity analysis This also works as a flag to determine if the purity analysis has already been performed: If it is None, the purity analysis has not been performed

unknown_calls dict[NodeID, UnknownProto]

A dict of all unknown calls. Unknown calls are calls to functions that are not defined in the module or are parameters.

Source code in src/library_analyzer/processing/api/purity_analysis/model/_reference.py
@dataclass
class Reasons:
    """
    Represents a function and the raw reasons for impurity.

    Raw reasons means that the reasons are just collected and not yet processed.

    Attributes
    ----------
    function_scope :
        The scope of the function which the reasons belong to.
        Is None if the reasons are not for a FunctionDef node.
        This is the case when either a builtin or a combined node is created,
        or a ClassScope is used to propagate reasons.
    writes_to :
        A dict of all nodes that are written to.
    reads_from :
        A dict of all nodes that are read from.
    calls :
        A set of all nodes that are called.
    result :
        The result of the purity analysis
        This also works as a flag to determine if the purity analysis has already been performed:
        If it is None, the purity analysis has not been performed
    unknown_calls :
        A dict of all unknown calls.
        Unknown calls are calls to functions that are not defined in the module or are parameters.
    """

    id: NodeID
    function_scope: FunctionScope | None = field(default=None)
    writes_to: dict[NodeID, NonLocalVariableWrite] = field(default_factory=dict)
    reads_from: dict[NodeID, NonLocalVariableRead] = field(default_factory=dict)
    calls: set[Symbol] = field(default_factory=set)  # TODO: SORTED SET oder LIST
    result: PurityResult | None = field(default=None)
    unknown_calls: dict[NodeID, UnknownProto] = field(default_factory=dict)

    def join_reasons_list(self, reasons_list: list[Reasons]) -> Reasons:
        """Join a list of Reasons objects.

        Combines a list of Reasons objects into one Reasons object.

        Parameters
        ----------
        reasons_list :
            The list of Reasons objects.


        Returns
        -------
        Reasons
            The combined Reasons object.

        Raises
        ------
        ValueError
            If the list of Reasons objects is empty.
        """
        if not reasons_list:
            raise ValueError("List of Reasons is empty.")

        result = self
        for reason in reasons_list:
            result.join_reasons(reason)
        return result

    def join_reasons(self, other: Reasons) -> Reasons:
        """Join two Reasons objects.

        When a function has multiple reasons for impurity, the Reasons objects are joined.
        This means that the writes, reads, calls and unknown_calls are merged.

        Parameters
        ----------
        other :
            The other Reasons object.

        Returns
        -------
        Reasons
            The updated Reasons object.
        """
        self.writes_to.update(other.writes_to)
        self.reads_from.update(other.reads_from)
        self.calls.update(other.calls)
        self.unknown_calls.update(other.unknown_calls)

        return self

    def remove_unknown_call(self, node_id: NodeID) -> None:
        """Remove an unknown call from the reasons.

        Parameters
        ----------
        node_id :
            The NodeID of the unknown call to remove.
        """
        del self.unknown_calls[node_id]

calls: set[Symbol] = field(default_factory=set) class-attribute instance-attribute

function_scope: FunctionScope | None = field(default=None) class-attribute instance-attribute

id: NodeID instance-attribute

reads_from: dict[NodeID, NonLocalVariableRead] = field(default_factory=dict) class-attribute instance-attribute

result: PurityResult | None = field(default=None) class-attribute instance-attribute

unknown_calls: dict[NodeID, UnknownProto] = field(default_factory=dict) class-attribute instance-attribute

writes_to: dict[NodeID, NonLocalVariableWrite] = field(default_factory=dict) class-attribute instance-attribute

__init__(id, function_scope=None, writes_to=dict(), reads_from=dict(), calls=set(), result=None, unknown_calls=dict())

join_reasons(other)

Join two Reasons objects.

When a function has multiple reasons for impurity, the Reasons objects are joined. This means that the writes, reads, calls and unknown_calls are merged.

Parameters:

Name Type Description Default
other Reasons

The other Reasons object.

required

Returns:

Type Description
Reasons

The updated Reasons object.

Source code in src/library_analyzer/processing/api/purity_analysis/model/_reference.py
def join_reasons(self, other: Reasons) -> Reasons:
    """Join two Reasons objects.

    When a function has multiple reasons for impurity, the Reasons objects are joined.
    This means that the writes, reads, calls and unknown_calls are merged.

    Parameters
    ----------
    other :
        The other Reasons object.

    Returns
    -------
    Reasons
        The updated Reasons object.
    """
    self.writes_to.update(other.writes_to)
    self.reads_from.update(other.reads_from)
    self.calls.update(other.calls)
    self.unknown_calls.update(other.unknown_calls)

    return self

join_reasons_list(reasons_list)

Join a list of Reasons objects.

Combines a list of Reasons objects into one Reasons object.

Parameters:

Name Type Description Default
reasons_list list[Reasons]

The list of Reasons objects.

required

Returns:

Type Description
Reasons

The combined Reasons object.

Raises:

Type Description
ValueError

If the list of Reasons objects is empty.

Source code in src/library_analyzer/processing/api/purity_analysis/model/_reference.py
def join_reasons_list(self, reasons_list: list[Reasons]) -> Reasons:
    """Join a list of Reasons objects.

    Combines a list of Reasons objects into one Reasons object.

    Parameters
    ----------
    reasons_list :
        The list of Reasons objects.


    Returns
    -------
    Reasons
        The combined Reasons object.

    Raises
    ------
    ValueError
        If the list of Reasons objects is empty.
    """
    if not reasons_list:
        raise ValueError("List of Reasons is empty.")

    result = self
    for reason in reasons_list:
        result.join_reasons(reason)
    return result

remove_unknown_call(node_id)

Remove an unknown call from the reasons.

Parameters:

Name Type Description Default
node_id NodeID

The NodeID of the unknown call to remove.

required
Source code in src/library_analyzer/processing/api/purity_analysis/model/_reference.py
def remove_unknown_call(self, node_id: NodeID) -> None:
    """Remove an unknown call from the reasons.

    Parameters
    ----------
    node_id :
        The NodeID of the unknown call to remove.
    """
    del self.unknown_calls[node_id]