Skip to content

Import

Bases: Symbol

Represents an import.

Attributes:

Name Type Description
node ImportFrom | Import

The node that defines the import.

name

The name of the symbol that is imported if any is given. Else it is equal to the module name.

module str

The name of the module that is imported.

alias str | None

If the node is of type Import alias is the alias name for the module name if any is given. If the node is of type ImportFrom alias is the alias name for the name of the symbol if any is given.

inferred_node NodeNG | None

When the import is used as a reference (or a symbol) the inferred_node is the node of the used reference (or symbol) in the original module. It was inferred by the reference analysis by using astroids safe_infer method. If the method could not infer the node, the inferred_node is None.

call Call | None

The original call node as fallback for the case, that the purity of the inferred_node cannot be inferred. Only is set if the symbol represents a call.

Source code in src/library_analyzer/processing/api/purity_analysis/model/_module_data.py
@dataclass
class Import(Symbol):
    """Represents an import.

    Attributes
    ----------
    node :
        The node that defines the import.
    name :
        The name of the symbol that is imported if any is given.
        Else it is equal to the module name.
    module :
        The name of the module that is imported.
    alias :
        If the node is of type Import alias is the alias name for the module name if any is given.
        If the node is of type ImportFrom alias is the alias name for the name of the symbol if any is given.
    inferred_node :
        When the import is used as a reference (or a symbol)
        the inferred_node is the node of the used reference (or symbol) in the original module.
        It was inferred by the reference analysis by using astroids safe_infer method.
        If the method could not infer the node, the inferred_node is None.
    call :
        The original call node as fallback for the case, that the purity of the inferred_node cannot be inferred.
        Only is set if the symbol represents a call.
    """

    node: astroid.ImportFrom | astroid.Import
    module: str
    alias: str | None = None
    inferred_node: astroid.NodeNG | None = None
    call: astroid.Call | None = None

    def __str__(self) -> str:
        if isinstance(self.node, astroid.ImportFrom):
            if self.name:
                return f"{self.__class__.__name__}.{self.module}.{self.name}.line{self.id.line}"
            return f"{self.__class__.__name__}.{self.module}.line{self.id.line}"
        else:
            if self.name != self.module:
                return f"{self.__class__.__name__}.{self.module}.{self.name}.line{self.id.line}"
            return f"{self.__class__.__name__}.{self.module}.line{self.id.line}"

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

alias: str | None = None class-attribute instance-attribute

call: astroid.Call | None = None class-attribute instance-attribute

inferred_node: astroid.NodeNG | None = None class-attribute instance-attribute

module: str instance-attribute

node: astroid.ImportFrom | astroid.Import instance-attribute

__hash__()

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

__init__(node, id, name, module, alias=None, inferred_node=None, call=None)

__str__()

Source code in src/library_analyzer/processing/api/purity_analysis/model/_module_data.py
def __str__(self) -> str:
    if isinstance(self.node, astroid.ImportFrom):
        if self.name:
            return f"{self.__class__.__name__}.{self.module}.{self.name}.line{self.id.line}"
        return f"{self.__class__.__name__}.{self.module}.line{self.id.line}"
    else:
        if self.name != self.module:
            return f"{self.__class__.__name__}.{self.module}.{self.name}.line{self.id.line}"
        return f"{self.__class__.__name__}.{self.module}.line{self.id.line}"