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.
| 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.
| 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.
Thanks for catching this!
There was a problem hiding this comment.
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.
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. |
|
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
KeyErrorthat 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.