From 63779fea00ab81b2caa051b49139a22800f3762c Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Tue, 18 Mar 2025 17:51:49 +0530 Subject: [PATCH] Make callable-related MethodError messages more descriptive --- base/errorshow.jl | 6 ++++-- test/errorshow.jl | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/base/errorshow.jl b/base/errorshow.jl index b62801b2fa59b..e9f8a9a30db74 100644 --- a/base/errorshow.jl +++ b/base/errorshow.jl @@ -291,7 +291,9 @@ function showerror(io::IO, ex::MethodError) elseif isempty(methods(f)) && isa(f, DataType) && isabstracttype(f) print(io, "no constructors have been defined for ", f) elseif isempty(methods(f)) && !isa(f, Function) && !isa(f, Type) - print(io, "objects of type ", ft, " are not callable") + println(io, "objects of type ", ft, " are not callable.") + print(io, "In case you did not try calling it explicitly, check if a ", ft, + " has been passed as an argument to a method that expects a callable instead.") else if ft <: Function && isempty(ft.parameters) && _isself(ft) f_is_function = true @@ -323,7 +325,7 @@ function showerror(io::IO, ex::MethodError) end end if ft <: AbstractArray - print(io, "\nUse square brackets [] for indexing an Array.") + print(io, "\nIn case you're trying to index into the array, use square brackets [] instead of parentheses ().") end # Check for local functions that shadow methods in Base let name = ft.name.mt.name diff --git a/test/errorshow.jl b/test/errorshow.jl index 5e362717ede09..8d582ba8e538c 100644 --- a/test/errorshow.jl +++ b/test/errorshow.jl @@ -419,7 +419,10 @@ let err_str, err_str = @except_str FunctionLike()() MethodError @test occursin("MethodError: no method matching (::$(curmod_prefix)FunctionLike)()", err_str) err_str = @except_str [1,2](1) MethodError - @test occursin("MethodError: objects of type Vector{$Int} are not callable\nUse square brackets [] for indexing an Array.", err_str) + @test occursin("MethodError: objects of type Vector{$Int} are not callable.\n"* + "In case you did not try calling it explicitly, check if a Vector{$Int}"* + " has been passed as an argument to a method that expects a callable instead.\n"* + "In case you're trying to index into the array, use square brackets [] instead of parentheses ().", err_str) # Issue 14940 err_str = @except_str randn(1)() MethodError @test occursin("MethodError: objects of type Vector{Float64} are not callable", err_str)