Tools and packages to automate type stability checks?

If I understand correctly, return type annotations cannot meaningfully help with type stability.

There is also @code_warntype, @inferred, Cthulhu.jl and TypeStability.jl, but they all require pointwise manual macrosing or explicit function calls with various arguments to check the macrosed function return types.

Is there any tool to automate such checks via, for example, fuzzy testing or some kind “static-typed mode”, so that there is a warning for functions that are suspected to be type unstable without the need to prefix every function manually?

1 Like
4 Likes

Type stability is a property of a function call, not a method or a function (which can have multiple methods). That’s because the types of the function and all its arguments determine which method is dispatched to and how that method is compiled, which includes type inference of the variables. Here’s a simple example:

julia> function maybestable(x)
         if x isa Int
           rand(("negative", false, 0, nothing, missing))
         else
           true
         end
       end
maybestable (generic function with 1 method)

julia> using Test

julia> @inferred maybestable(1) # return type varies across calls
ERROR: return type Int64 does not match inferred return type Any
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] top-level scope
   @ REPL[8]:1

julia> @inferred maybestable(1.5)
true
3 Likes