function_call_analyzer¶
Function Call Analyzer Module.
This module provides functionality to analyze function definitions and calls in Python code using LibCST. It helps identify unused functions and analyze function call patterns in a codebase.
Example
>>> from haive.tools.toolkits.dev.python.cst_toolkit.visitors.function_call_analyzer import analyze_function_calls
>>> analyze_function_calls("/path/to/file.py")
Unused Functions: ['initialize_config', 'cleanup_resources']
Classes¶
Tracks function definitions and calls within a script. |
Functions¶
|
Analyze function definitions and calls in a Python file. |
Module Contents¶
- class function_call_analyzer.FunctionCallAnalyzer¶
Bases:
libcst.CSTVisitor
Tracks function definitions and calls within a script.
This visitor analyzes Python code to identify function definitions and track each time a function is called, allowing for the detection of unused functions and function call patterns.
- report_unused_functions() list[str] ¶
Find functions that are defined but never called.
- Returns:
- List of function names that are defined but never called
within the analyzed code
- Return type:
List[str]
- visit_Call(node: libcst.Call) None ¶
Track function calls during the AST traversal.
This method increments the call count for functions when they are invoked. It only tracks direct function calls by name, not method calls or calls through variables.
- Parameters:
node (Call) – The function call node being visited
- function_call_analyzer.analyze_function_calls(filepath: str) dict[str, list[str]] ¶
Analyze function definitions and calls in a Python file.
This function parses a Python file, analyzes function definitions and calls, and reports various metrics including unused functions.
- Parameters:
filepath (str) – Path to the Python file to analyze
- Returns:
Dictionary with analysis results, including ‘unused_functions’
- Return type:
- Raises:
FileNotFoundError – If the specified file does not exist
IOError – If there are issues reading from the file