AB.TypespecCorrector (AB v0.2.1)

View Source

Analyzes failed tests and suggests corrected typespecs based on runtime values.

This module infers types from actual runtime values and suggests corrections when a function's behavior doesn't match its declared typespec.

Summary

Functions

Analyzes a failed test using runtime values and suggests a corrected typespec.

Functions

suggest_typespec_correction(module, function_name, actual_inputs, actual_result, mismatch_type)

@spec suggest_typespec_correction(module(), atom(), list(), any(), atom()) ::
  %{
    old_spec: String.t(),
    new_spec: String.t(),
    old_spec_ast: {[any()], any()},
    new_spec_ast: {[any()], any()},
    reason: String.t()
  }
  | {:error, String.t()}

Analyzes a failed test using runtime values and suggests a corrected typespec.

Takes actual runtime values (inputs and result) along with the current typespec, infers the correct types from the values, and suggests a corrected typespec.

Parameters

  • module: The module containing the function
  • function_name: The name of the function that failed
  • actual_inputs: List of actual input values used in the test
  • actual_result: The actual result value returned by the function
  • mismatch_type: Either :return or :argument to indicate which type mismatched

Returns

A map containing:

  • :old_spec - The current typespec as a string
  • :new_spec - The suggested corrected typespec as a string
  • :old_spec_ast - The current typespec as AST {input_types, output_type}
  • :new_spec_ast - The suggested typespec as AST {input_types, output_type}
  • :reason - Why the typespec needs correction

Example

iex> AB.TypespecCorrector.suggest_typespec_correction(NumberFunctions, :double, [1.0], 2.0, :return)
%{
  old_spec: "@spec double(number()) :: binary()",
  new_spec: "@spec double(number()) :: float()",
  old_spec_ast: {[{:type, 0, :number, []}], {:type, 0, :binary, []}},
  new_spec_ast: {[{:type, 0, :number, []}], {:type, 0, :float, []}},
  reason: "Return type mismatch: function returns float() but typespec declares binary()"
}