dependency_analyzer¶
Dependency Analyzer Module.
This module provides functionality to analyze import statements and track dependencies between modules in Python projects using LibCST. It helps identify the external libraries and internal modules that a Python file depends on.
Example
>>> from haive.tools.toolkits.dev.python.cst_toolkit.visitors.dependency_analyzer import analyze_dependencies
>>> analyze_dependencies("/path/to/file.py")
Dependencies in /path/to/file.py: {'os', 'sys', 'pandas', 'numpy', 'myproject.utils'}
Classes¶
Analyzes imports and tracks dependencies within a project. |
Functions¶
|
Analyze import dependencies in a Python file. |
Module Contents¶
- class dependency_analyzer.DependencyAnalyzer(file_path: str | None = None)¶
Bases:
libcst.CSTVisitor
Analyzes imports and tracks dependencies within a project.
This visitor identifies and collects all import statements in a Python file, including both direct imports and imports from specific modules, to build a dependency graph of the codebase.
- get_dependencies() set[str] ¶
Get the set of dependencies for the current file.
- Returns:
Set of module names imported in the current file
- Return type:
Set[str]
- visit_Import(node: libcst.Import) None ¶
Handle simple ‘import module’ statements during the AST traversal.
This method extracts module names from import statements and adds them to the dependency set for the current file.
- Parameters:
node (cst.Import) – The import node being visited
- visit_ImportFrom(node: libcst.ImportFrom) None ¶
Handle ‘from module import X’ statements during the AST traversal.
This method extracts the base module name from import-from statements and adds it to the dependency set for the current file.
- Parameters:
node (cst.ImportFrom) – The import-from node being visited
- current_file = 'current_file'¶
- imports¶
- dependency_analyzer.analyze_dependencies(filepath: str) set[str] ¶
Analyze import dependencies in a Python file.
This function parses a Python file and identifies all the modules it imports, returning a set of dependency module names.
- Parameters:
filepath (str) – Path to the Python file to analyze
- Returns:
Set of module names imported in the file
- Return type:
Set[str]
- Raises:
FileNotFoundError – If the specified file does not exist
IOError – If there are issues reading from the file