import_analyzer

Import Analyzer Module.

This module provides functionality to analyze import statements in Python code using LibCST. It helps identify unused imports, import conflicts, and other import-related issues that can affect code quality and maintainability.

Example

>>> from haive.tools.toolkits.dev.python.cst_toolkit.visitors.import_analyzer import analyze_imports
>>> analyze_imports("/path/to/file.py")
📌 **Import Analysis Report**
✅ Used Imports: {'os', 'sys', 'pandas'}
🛑 Unused Imports: {'numpy', 'matplotlib'}
⚠️ Conflict: DataFrame is imported from multiple sources: {'pandas', 'pyspark.sql'}

Classes

ImportAnalyzer

Analyzes and detects issues in Python imports.

Functions

analyze_imports(→ dict[str, list])

Analyze import statements in a Python file.

Module Contents

class import_analyzer.ImportAnalyzer

Bases: libcst.CSTVisitor

Analyzes and detects issues in Python imports.

This visitor analyzes import statements in Python code to identify issues such as unused imports, import conflicts, and other import-related problems that can affect code quality.

imports

Set of all imported module names

Type:

Set[str]

import_conflicts

Dictionary mapping imported names to their source modules

Type:

Dict[str, Set[str]]

unused_imports

Set of imported names that are not used in the code

Type:

Set[str]

used_identifiers

Set of identifier names used in the code

Type:

Set[str]

report_issues() dict[str, list]

Generate a report on import-related issues.

This method prints a human-readable report of import issues and returns a structured representation of the findings.

Returns:

Dictionary containing analysis results, including

’unused_imports’, ‘import_conflicts’, and ‘all_imports’

Return type:

Dict[str, List]

visit_Import(node: libcst.Import) None

Track imported modules during the AST traversal.

This method processes ‘import module’ statements and records information about imported modules, potential conflicts, and usage tracking.

Parameters:

node (cst.Import) – The import node being visited

visit_ImportFrom(node: libcst.ImportFrom) None

Track ‘from module import x’ statements during the AST traversal.

This method processes import-from statements and records information about imported names, their source modules, and usage tracking.

Parameters:

node (cst.ImportFrom) – The import-from node being visited

visit_Name(node: libcst.Name) None

Detect used symbols in the script during the AST traversal.

This method tracks which imported names are actually used in the code to identify unused imports.

Parameters:

node (cst.Name) – The name node being visited

import_conflicts: dict[str, set[str]]
imports: set[str]
unused_imports: set[str]
used_identifiers: set[str]
import_analyzer.analyze_imports(filepath: str) dict[str, list]

Analyze import statements in a Python file.

This function parses a Python file and analyzes its import statements to identify unused imports, import conflicts, and other import-related issues.

Parameters:

filepath (str) – Path to the Python file to analyze

Returns:

Dictionary containing analysis results, including

’unused_imports’, ‘import_conflicts’, and ‘all_imports’

Return type:

Dict[str, List]

Raises: