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

Give a better error message for stack(array1, array2) #57807

Open
digital-carver opened this issue Mar 17, 2025 · 0 comments
Open

Give a better error message for stack(array1, array2) #57807

digital-carver opened this issue Mar 17, 2025 · 0 comments

Comments

@digital-carver
Copy link
Contributor

digital-carver commented Mar 17, 2025

It's an easy mistake to make to call stack(array1, array2) instead of stack([array1, array2]) - for eg., having used stack only occasionally, I just tried (something similar to) stack(1:3, 4:6), and got the error message:

ERROR: MethodError: objects of type UnitRange{Int64} are not callable
Use square brackets [] for indexing an Array.
The object of type `UnitRange{Int64}` exists, but no method is defined for this combination of argument types when trying to treat it as a callable object.
Stacktrace:
...

This was really confusing, and had me even checking if I'd accidentally declared a variable named stack somehow. Even looking at the docstring didn't immediately make it obvious why I was getting this particular error, until I realized that it was hitting the

stack(f, args...; [dims])

dispatch, and trying to use 1:3 as the callable f.

It would be a nice improvement to DevEx if this subtle and easy-to-make mistake had a better error message, that more directly hinted at the problem. Something like:

If you're passing multiple iterables to stack, pass them instead as a single iterable of iterables i.e. instead of stack(array1, array2), use stack([array1, array2])

At a glance,

function methods_on_iterable(io, ex, arg_types, kwargs)
(methods_on_iterable in errorshow.jl) seems like a convenient place to add this.

jishnub added a commit that referenced this issue Mar 19, 2025
Addresses #57807 to some
extent, although the error message may be improved further.

On nightly,
```julia
julia> stack(1:3, 1:3)
ERROR: MethodError: objects of type UnitRange{Int64} are not callable
Use square brackets [] for indexing an Array.
The object of type `UnitRange{Int64}` exists, but no method is defined for this combination of argument types when trying to treat it as a callable object.
```
The second sentence seems unrelated to the first, and the source of the
error remains unclear from the message.

This PR makes the message more verbose, and provides pointers.
```julia
julia> stack(1:3, 200)
ERROR: MethodError: objects of type UnitRange{Int64} are not callable.
In case you did not try calling it explicitly, check if a UnitRange{Int64} has been passed as an argument to a method that expects a callable instead.
In case you're trying to index into the array, use square brackets [] instead of parentheses ().
The object of type `UnitRange{Int64}` exists, but no method is defined for this combination of argument types when trying to treat it as a callable object.

```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant