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ยถ

ComplexityAnalyzer

Analyzes cyclomatic complexity of functions in Python code.

Functionsยถ

analyze_complexity(โ†’ dict[str, int])

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: