AB.TypespecCorrector (AB v0.2.1)
View SourceAnalyzes 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
@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 functionfunction_name: The name of the function that failedactual_inputs: List of actual input values used in the testactual_result: The actual result value returned by the functionmismatch_type: Either:returnor:argumentto 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()"
}