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¶
Analyzes and detects issues in Python imports. |
Functions¶
|
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.
- import_conflicts¶
Dictionary mapping imported names to their source modules
- 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
- 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:
FileNotFoundError – If the specified file does not exist
IOError – If there are issues reading from the file