Skip to content

Commit 54114f7

Browse files
committed
Make callable-related MethodError messages more descriptive
1 parent 7ee404c commit 54114f7

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

base/errorshow.jl

+4-2
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,9 @@ function showerror(io::IO, ex::MethodError)
291291
elseif isempty(methods(f)) && isa(f, DataType) && isabstracttype(f)
292292
print(io, "no constructors have been defined for ", f)
293293
elseif isempty(methods(f)) && !isa(f, Function) && !isa(f, Type)
294-
print(io, "objects of type ", ft, " are not callable")
294+
println(io, "objects of type ", ft, " are not callable.")
295+
print(io, "In case you did not try calling it explicitly, check if a ", ft,
296+
" has been passed as an argument to a method that expects a callable instead.")
295297
else
296298
if ft <: Function && isempty(ft.parameters) && _isself(ft)
297299
f_is_function = true
@@ -323,7 +325,7 @@ function showerror(io::IO, ex::MethodError)
323325
end
324326
end
325327
if ft <: AbstractArray
326-
print(io, "\nUse square brackets [] for indexing an Array.")
328+
print(io, "\nIn case you're trying to index into the array, use square brackets [] instead of parentheses ().")
327329
end
328330
# Check for local functions that shadow methods in Base
329331
let name = ft.name.mt.name

test/errorshow.jl

+4-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,10 @@ let err_str,
419419
err_str = @except_str FunctionLike()() MethodError
420420
@test occursin("MethodError: no method matching (::$(curmod_prefix)FunctionLike)()", err_str)
421421
err_str = @except_str [1,2](1) MethodError
422-
@test occursin("MethodError: objects of type Vector{$Int} are not callable\nUse square brackets [] for indexing an Array.", err_str)
422+
@test occursin("MethodError: objects of type Vector{$Int} are not callable.\n"*
423+
"In case you did not try calling it explicitly, check if a Vector{$Int}"*
424+
" has been passed as an argument to a method that expects a callable instead.\n"*
425+
"In case you're trying to index into the array, use square brackets [] instead of parentheses ().", err_str)
423426
# Issue 14940
424427
err_str = @except_str randn(1)() MethodError
425428
@test occursin("MethodError: objects of type Vector{Float64} are not callable", err_str)

0 commit comments

Comments
 (0)