code_smell_detector

Code Smell Detector Module.

This module provides functionality to detect code smells and bad practices in Python code using LibCST. It identifies potential issues such as deeply nested loops that can make code harder to understand and maintain.

Example

>>> from haive.tools.toolkits.dev.python.cst_toolkit.visitors.code_smell_detector import detect_code_smells
>>> detect_code_smells("/path/to/file.py")
⚠️ Deeply nested loop detected at line 42

Classes

CodeSmellDetector

Detects code smells and bad coding patterns.

Functions

detect_code_smells(→ list[dict])

Detect code smells in a Python file.

Module Contents

class code_smell_detector.CodeSmellDetector

Bases: libcst.CSTVisitor

Detects code smells and bad coding patterns.

This visitor analyzes Python code for potential code smells, such as deeply nested loops, excessively large functions, and other anti-patterns that can impact code maintainability and readability.

nesting_level

The current nesting level of loops being analyzed

Type:

int

issues

List of detected code smells with details

Type:

List[Dict]

get_issues() list[dict]

Return a list of all detected code smells.

Returns:

List of dictionaries containing details about each detected issue

Return type:

List[Dict]

leave_For(original_node: libcst.For) None

Decrement nesting level when leaving a for loop.

Parameters:

original_node (cst.For) – The for loop node being left

visit_For(node: libcst.For) None

Track nesting depth of for loops during the AST traversal.

This method increments the nesting level counter when entering a loop and checks if the nesting level exceeds a threshold (3).

Parameters:

node (cst.For) – The for loop node being visited

issues: list[dict] = []
nesting_level = 0
code_smell_detector.detect_code_smells(filepath: str) list[dict]

Detect code smells in a Python file.

This function analyzes a Python file to identify potential code smells and bad practices that could impact code quality and maintainability.

Parameters:

filepath (str) – Path to the Python file to analyze

Returns:

List of detected code smells with details

Return type:

List[Dict]

Raises: