Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make callable-related MethodError messages more descriptive #57815

Merged
merged 1 commit into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion test/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down