JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.59k stars 5.48k forks source link

Fix error messages for ranges with missing #46245

Open bkamins opened 2 years ago

bkamins commented 2 years ago

@nalimilan - this is a common error.

First a case when I think error message is OK (or at least acceptable):

julia> missing:1:missing
ERROR: TypeError: non-boolean (Missing) used in boolean context

Now a case when new users will be confused:

julia> missing:0.1:missing
ERROR: ArgumentError: StepRange should not be used with floating point

Source why it is common:

julia> x = [missing, 1.1, 0.3]
3-element Vector{Union{Missing, Float64}}:
  missing
 1.1
 0.3

julia> minimum(x):0.1:maximum(x) # clearly skipmisssing should be used but it is not obvious to the user
ERROR: ArgumentError: StepRange should not be used with floating point
nalimilan commented 2 years ago

Good point. I'd say that the problem here is that the x:y:z syntax dispatches to StepRange despite the fact that y is floating point. An error should probably be thrown earlier, without mentioning StepRange, since the user didn't ask for it at any point. Printing the type of all three values would help users understand the problem.

bkamins commented 2 years ago

Yes, the reason is that the method upstream expects x and z to have the same type only. Most likely we need to add a method with Missing being a specific type and throw an error in that case. BTW a related issue is https://github.com/JuliaLang/julia/issues/46244.

Note that also:

julia> 0.1:0.1:missing
ERROR: MethodError: no method matching (::Colon)(::Float64, ::Float64, ::Missing)

is I think OK.