complexity_analyzerยถ
Complexity Analyzer Module.
This module provides a LibCST visitor that analyzes the cyclomatic complexity of functions in Python code. Cyclomatic complexity is a quantitative measure of the number of linearly independent paths through a programโs source code, which can help identify functions that may be overly complex and difficult to maintain.
The analyzer tracks complexity by counting decision points (if statements, loops, etc.) within each function and provides a report with complexity scores, which can be used to identify functions that might benefit from refactoring.
Examples
>>> from haive.tools.toolkits.dev.python.cst_toolkit.visitors.complexity_analyzer import analyze_complexity
>>> analyze_complexity("path/to/your_script.py")
๐ function_name complexity score: 5
Classesยถ
Analyzes cyclomatic complexity of functions in Python code. |
Functionsยถ
|
Analyze the cyclomatic complexity of functions in a Python file. |
Module Contentsยถ
- class complexity_analyzer.ComplexityAnalyzerยถ
Bases:
libcst.CSTVisitor
Analyzes cyclomatic complexity of functions in Python code.
This visitor traverses the CST of Python code and calculates the cyclomatic complexity of each function by counting decision points like if statements, loops, and boolean operations.
- complexityยถ
Dictionary mapping function names to complexity scores.
- current_functionยถ
Name of the function currently being analyzed.
- nested_functionsยถ
Stack of nested function names being analyzed.
- get_high_complexity_functions(threshold: int = 10) dict[str, int] ยถ
Identify functions with complexity above a threshold.
- Parameters:
threshold โ The complexity threshold above which functions are considered complex.
- Returns:
Dictionary of function names and scores for functions exceeding the threshold.
- leave_FunctionDef(original_node: libcst.FunctionDef) None ยถ
Handle exiting a function definition.
This method is called when the visitor leaves a function definition. It restores the previous current function if we were in a nested function.
- Parameters:
original_node โ The function definition node in the CST.
- report_complexity() dict[str, int] ยถ
Generate a report of function complexity scores.
- Returns:
Dictionary mapping function names to complexity scores.
- visit_BooleanOperation(node: libcst.BooleanOperation) None ยถ
Increase complexity for boolean operations.
Each boolean operation (and, or) adds 1 to the cyclomatic complexity.
- Parameters:
node โ The boolean operation node in the CST.
- visit_For(node: libcst.For) None ยถ
Increase complexity for for loops.
Each loop adds 1 to the cyclomatic complexity.
- Parameters:
node โ The for loop node in the CST.
- visit_FunctionDef(node: libcst.FunctionDef) None ยถ
Track function definitions and initialize complexity.
This method is called when the visitor enters a function definition. It sets the current function being analyzed and initializes its complexity score to 1.
- Parameters:
node โ The function definition node in the CST.
- visit_If(node: libcst.If) None ยถ
Increase complexity for conditional branches.
Each if/elif/else branch adds 1 to the cyclomatic complexity.
- Parameters:
node โ The if statement node in the CST.
- visit_Try(node: libcst.Try) None ยถ
Increase complexity for try/except blocks.
Each except handler adds 1 to the cyclomatic complexity.
- Parameters:
node โ The try statement node in the CST.
- visit_While(node: libcst.While) None ยถ
Increase complexity for while loops.
Each loop adds 1 to the cyclomatic complexity.
- Parameters:
node โ The while loop node in the CST.
- complexityยถ
- current_function = Noneยถ
- nested_functions = []ยถ
- complexity_analyzer.analyze_complexity(filepath: str, threshold: int | None = None) dict[str, int] ยถ
Analyze the cyclomatic complexity of functions in a Python file.
- Parameters:
filepath โ Path to the Python file to analyze.
threshold โ Optional complexity threshold for reporting high-complexity functions. If provided, only functions exceeding this threshold will be reported.
- Returns:
Dictionary mapping function names to complexity scores.
- Raises:
FileNotFoundError โ If the specified file does not exist.
SyntaxError โ If the file contains invalid Python syntax.