# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt
"""This module contains some base nodes that can be inherited for the different nodes.
Previously these were called Mixin nodes.
"""
from __future__ import annotations
import itertools
import sys
from collections.abc import Iterator
from typing import TYPE_CHECKING, ClassVar
from astroid import decorators
from astroid.exceptions import AttributeInferenceError
from astroid.nodes.node_ng import NodeNG
if TYPE_CHECKING:
from astroid import nodes
if sys.version_info >= (3, 8):
from functools import cached_property
else:
from astroid.decorators import cachedproperty as cached_property
class Statement(NodeNG):
"""Statement node adding a few attributes.
NOTE: This class is part of the public API of 'astroid.nodes'.
"""
is_statement = True
"""Whether this node indicates a statement."""
def next_sibling(self):
"""The next sibling statement node.
:returns: The next sibling statement node.
:rtype: NodeNG or None
"""
stmts = self.parent.child_sequence(self)
index = stmts.index(self)
try:
return stmts[index + 1]
except IndexError:
return None
def previous_sibling(self):
"""The previous sibling statement.
:returns: The previous sibling statement node.
:rtype: NodeNG or None
"""
stmts = self.parent.child_sequence(self)
index = stmts.index(self)
if index >= 1:
return stmts[index - 1]
return None
class NoChildrenNode(NodeNG):
"""Base nodes for nodes with no children, e.g. Pass."""
def get_children(self) -> Iterator[NodeNG]:
yield from ()
class FilterStmtsBaseNode(NodeNG):
"""Base node for statement filtering and assignment type."""
def _get_filtered_stmts(self, _, node, _stmts, mystmt: Statement | None):
"""Method used in _filter_stmts to get statements and trigger break."""
if self.statement(future=True) is mystmt:
# original node's statement is the assignment, only keep
# current node (gen exp, list comp)
return [node], True
return _stmts, False
def assign_type(self):
return self
class AssignTypeNode(NodeNG):
"""Base node for nodes that can 'assign' such as AnnAssign."""
def assign_type(self):
return self
def _get_filtered_stmts(self, lookup_node, node, _stmts, mystmt: Statement | None):
"""Method used in filter_stmts."""
if self is mystmt:
return _stmts, True
if self.statement(future=True) is mystmt:
# original node's statement is the assignment, only keep
# current node (gen exp, list comp)
return [node], True
return _stmts, False
class ParentAssignNode(AssignTypeNode):
"""Base node for nodes whose assign_type is determined by the parent node."""
def assign_type(self):
return self.parent.assign_type()
class ImportNode(FilterStmtsBaseNode, NoChildrenNode, Statement):
"""Base node for From and Import Nodes."""
modname: str | None
"""The module that is being imported from.
This is ``None`` for relative imports.
"""
names: list[tuple[str, str | None]]
"""What is being imported from the module.
Each entry is a :class:`tuple` of the name being imported,
and the alias that the name is assigned to (if any).
"""
def _infer_name(self, frame, name):
return name
def do_import_module(self, modname: str | None = None) -> nodes.Module:
"""Return the ast for a module whose name is