-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
error hint for getproperty(::Dict, ::Symbol) #54504
Changes from 17 commits
d6dd7ee
af793bd
2059bb6
cb03f1c
297a357
2675232
a0bb804
7f9fc8f
a879a18
cb1d5c8
fb1e0f7
752ac7c
5dfe5f8
9006635
a8ca205
2063b9b
7f09293
b1ef85f
4f96718
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -367,6 +367,12 @@ function showerror(io::IO, ex::MethodError) | |
nothing | ||
end | ||
|
||
function showerror(io::IO, exc::FieldError) | ||
@nospecialize | ||
print(io, "FieldError: type $(exc.type |> nameof) has no field $(exc.field)") | ||
Base.Experimental.show_error_hints(io, exc) | ||
end | ||
|
||
striptype(::Type{T}) where {T} = T | ||
striptype(::Any) = nothing | ||
|
||
|
@@ -1086,6 +1092,31 @@ end | |
|
||
Experimental.register_error_hint(methods_on_iterable, MethodError) | ||
|
||
# Display a hint in case the user tries to access non-member fields of container type datastructures | ||
function fielderror_hint_handler(io, exc) | ||
@nospecialize | ||
field = exc.field | ||
type = exc.type | ||
if type <: Dict | ||
println(io, | ||
""" | ||
\nDid you mean to access dict values using key: `:$field` ? | ||
Consider using indexing syntax. | ||
|
||
# Example | ||
julia> dict = Dict(:$(field)=>5) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems odd to use markdown syntax here since this will just get printed to stderr. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, the error hints tend to be quite short and to the point. Something like:
or something along those lines should be enough. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For most markdown syntax (e.g. code formatting), I agree with you (see above conversation in this PR). However, the single # header is also reasonable plaintext "syntax". This is a very niche error case (it only happens when you call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's good to keep the styling consistent with the rest of the hints for things to be cohesive. Following the example of |
||
Dict{Symbol, Int64} with 1 entry: | ||
:$(field) => 5 | ||
arhik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
julia> dict[:$(field)] | ||
5 | ||
""" | ||
) | ||
end | ||
end | ||
|
||
Experimental.register_error_hint(fielderror_hint_handler, FieldError) | ||
|
||
# ExceptionStack implementation | ||
size(s::ExceptionStack) = size(s.stack) | ||
getindex(s::ExceptionStack, i::Int) = s.stack[i] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be
AbstractDict
perhaps?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree. I could implement an AbstractDict that forwards getproperty to getindex.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be said "typically" then.