type_checking

Type Checking Visitor Module.

This module provides functionality to analyze Python code for type annotation issues using LibCST. It identifies functions missing type hints and incorrect return type annotations, helping to improve type safety in codebases.

Example

>>> from haive.tools.toolkits.dev.python.cst_toolkit.visitors.type_checking import check_types
>>> check_types("/path/to/file.py")
⚠️ Functions missing type hints: ['process_data', 'calculate_total']
❌ Incorrect return type in `format_result`: Expected str, got int

Classes

TypeChecker

Checks for missing or incorrect type hints in functions.

Functions

check_types(→ None)

Check a Python file for type annotation issues.

Module Contents

class type_checking.TypeChecker

Bases: libcst.CSTVisitor

Checks for missing or incorrect type hints in functions.

This visitor analyzes function definitions in Python code to identify missing parameter type annotations and incorrect return type annotations.

missing_annotations

List of function names missing type annotations

Type:

List[str]

incorrect_annotations

List of tuples containing function name, expected return type, and actual return type for functions with incorrect annotations

Type:

List[Tuple[str, str, str]]

function_types

Dictionary mapping function names to their expected return types

Type:

Dict[str, str]

report_issues() None

Print a report of all type checking issues found.

This method generates a human-readable report of functions missing type hints and functions with incorrect return type annotations.

visit_FunctionDef(node: libcst.FunctionDef) None

Check if a function has proper type hints during the AST traversal.

This method analyzes a function definition to determine if it has parameter type annotations and if the return type annotation is correct.

Parameters:

node (cst.FunctionDef) – The function definition node being visited

function_types: dict[str, str]
incorrect_annotations: list[tuple[str, str, str]] = []
missing_annotations: list[str] = []
type_checking.check_types(filepath: str) None

Check a Python file for type annotation issues.

This function analyzes a Python file to identify functions missing type annotations and functions with incorrect return type annotations, and prints a report of the issues.

Parameters:

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

Raises: