Skip to content

Scope

Represents a node in the scope tree.

The scope tree is a tree that represents the scope of a module. It is used to determine the scope of a reference. On the top level, there is a ScopeNode for the module. Each Scope has a list of children, which are the nodes that are defined in the scope of the node. Each Scope also has a reference to its parent node.

Attributes:

Name Type Description
_symbol Symbol

The symbol that defines the scope.

_children list[Scope]

The list of Scope or ClassScope instances that are defined in the scope of the Symbol node. Is None if the node is a leaf node.

_parent Scope | None

The parent node in the scope tree, there is None if the node is the root node.

Source code in src/library_analyzer/processing/api/purity_analysis/model/_module_data.py
@dataclass
class Scope:
    """Represents a node in the scope tree.

    The scope tree is a tree that represents the scope of a module. It is used to determine the scope of a reference.
    On the top level, there is a ScopeNode for the module. Each Scope has a list of children, which are the nodes
    that are defined in the scope of the node. Each Scope also has a reference to its parent node.

    Attributes
    ----------
    _symbol :
        The symbol that defines the scope.
    _children :
        The list of Scope or ClassScope instances that are defined in the scope of the Symbol node.
        Is None if the node is a leaf node.
    _parent :
        The parent node in the scope tree, there is None if the node is the root node.
    """

    _symbol: Symbol
    _children: list[Scope] = field(default_factory=list)
    _parent: Scope | None = None

    def __iter__(self) -> Iterator[Scope | ClassScope]:
        yield self

    def __next__(self) -> Scope | ClassScope:
        return self

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

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

    @property
    def symbol(self) -> Symbol:
        """Symbol : The symbol that defines the scope."""
        return self._symbol

    @symbol.setter
    def symbol(self, new_symbol: Symbol) -> None:
        if not isinstance(new_symbol, Symbol):
            raise TypeError("Invalid node type.")
        self._symbol = new_symbol

    @property
    def children(self) -> list[Scope | ClassScope]:
        """list[Scope | ClassScope] : Children of the scope.

        The list of Scope or ClassScope instances that are defined in the scope of the Symbol node.
        Is None if the node is a leaf node.
        """
        return self._children

    @children.setter
    def children(self, new_children: list[Scope | ClassScope]) -> None:
        if not isinstance(new_children, list):
            raise TypeError("Children must be a list.")
        self._children = new_children

    @property
    def parent(self) -> Scope | None:
        """Scope | ClassScope | None : Parent of the scope.

        The parent node in the scope tree.
        Is None if the node is the root node.
        """
        return self._parent

    @parent.setter
    def parent(self, new_parent: Scope | None) -> None:
        if not isinstance(new_parent, Scope | None):
            raise TypeError("Invalid parent type.")
        self._parent = new_parent

    def get_module_scope(self) -> Scope:
        """Return the module scope.

        Gets the module scope for each scope in the scope tree.
        The module scope is the root node of the scope tree.

        Returns
        -------
        Scope
            The module scope.
        """
        if self.parent is None:
            return self
        return self.parent.get_module_scope()

children: list[Scope | ClassScope] property writable

list[Scope | ClassScope] : Children of the scope.

The list of Scope or ClassScope instances that are defined in the scope of the Symbol node. Is None if the node is a leaf node.

parent: Scope | None property writable

Scope | ClassScope | None : Parent of the scope.

The parent node in the scope tree. Is None if the node is the root node.

symbol: Symbol property writable

Symbol : The symbol that defines the scope.

__hash__()

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

__init__(_symbol, _children=list(), _parent=None)

__iter__()

Source code in src/library_analyzer/processing/api/purity_analysis/model/_module_data.py
def __iter__(self) -> Iterator[Scope | ClassScope]:
    yield self

__next__()

Source code in src/library_analyzer/processing/api/purity_analysis/model/_module_data.py
def __next__(self) -> Scope | ClassScope:
    return self

__str__()

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

get_module_scope()

Return the module scope.

Gets the module scope for each scope in the scope tree. The module scope is the root node of the scope tree.

Returns:

Type Description
Scope

The module scope.

Source code in src/library_analyzer/processing/api/purity_analysis/model/_module_data.py
def get_module_scope(self) -> Scope:
    """Return the module scope.

    Gets the module scope for each scope in the scope tree.
    The module scope is the root node of the scope tree.

    Returns
    -------
    Scope
        The module scope.
    """
    if self.parent is None:
        return self
    return self.parent.get_module_scope()