-
-
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
Add object field to KeyError #55477
base: master
Are you sure you want to change the base?
Add object field to KeyError #55477
Conversation
Adding an optional field to KeyError that records the object the KeyError relates to allows for more sophisticated error messaging that can suggest potentially intended keys.
@@ -161,7 +161,7 @@ function parse_bool_env(name::String, val::String = ENV[name]; throw::Bool=false | |||
end | |||
end | |||
|
|||
getindex(::EnvDict, k::AbstractString) = access_env(k->throw(KeyError(k)), k) | |||
getindex(::EnvDict, k::AbstractString) = access_env(k->throw(KeyError(ENV, k)), k) |
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.
getindex(::EnvDict, k::AbstractString) = access_env(k->throw(KeyError(ENV, k)), k) | |
getindex(ENV::EnvDict, k::AbstractString) = access_env(k->throw(KeyError(ENV, k)), k) |
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.
Thanks for catching this!
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.
The assumption of ::EnvDict
referring to the global ENV
is also present in other functions (not written by you). I wonder if it deserve a separate issue? I guess, EnvDict
structure is a private API, still, if someone creates its own instance it basically won't really work properly with other functions anyway.
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.
EnvDict
is a singleton type, so it is fine to refer to "the global ENV
" as it is the only instance that can exist anyway.
Closes #35955? |
Not yet, I think once we have this type and use it for nicer messages we can close that issue. |
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.
Seems OK to me
I wonder what's the planned direction for exceptions in Julia? |
The distinction here (to me) is that |
Hmm, maybe I'm missing something, but this PR seems to be about KeyError, not FieldError. And runtime dictionary keys are the common case (unlike field names, there I agree). |
Also, CI is currently failing on this PR, and (based on a quick glance) it looks like the CI failures are related to the contents of this PR. Therefore, I'll remove the |
Ah yep, seems like a few tests also need to be touched up. I can get to those within the next day or two, but I'm not sure about
I'll take a look at it, but if anybody might have insight to share on that test, it would be appreciated. |
@aplavin that's a good point. I think I'd prefer it if this PR generated a summary |
Hmm, I see the concern with capturing the object, but I'd also be concerned about the overhead from doing the work to produce a helpful message up-front. With |
Adding an optional field to KeyError that records the object the KeyError relates to allows for more sophisticated error messaging that can suggest potentially intended keys.
As a first pass, I've passed the object in question into every instance of
KeyError
that I've seen so far, but I wouldn't be surprised if this may need to be omitted in some performance-critical cases where the potential of the object escaping the function inhibits key optimisations.