OmegaConf.to_container(..., throw_on_missing) keyword arg#730
OmegaConf.to_container(..., throw_on_missing) keyword arg#730Jasha10 merged 33 commits intoomry:masterfrom
Conversation
|
Just a note that it's going to take me a little while to get through this one (due to other priorities). |
odelalleau
left a comment
There was a problem hiding this comment.
Looks mostly good to me -- the main point to discuss is whether or not we want to allow interpolations to missing values when throw_on_missing is True
| try: | ||
| node = conf._get_node(key, throw_on_missing_value=throw_on_missing) | ||
| except MissingMandatoryValue as e: | ||
| conf._format_and_raise(key=key, value=None, cause=e) |
There was a problem hiding this comment.
Generally speaking, it's better to format exceptions at high level functions, otherwise you risk formatting them multiple times.
(e.g: in OmegaConf.to_container() or other high level functions calling _to_content()).
There was a problem hiding this comment.
Hmm... If I do formatting in OmegaConf.to_container(), then I will not be able to set the $KEY properly (since the for loop iterating through keys happens in this lower-level _to_content function).
There was a problem hiding this comment.
Is this related to this change? raise MissingMandatoryValue("Missing mandatory value: $KEY") ?
Why not just use the key directly when raising the exception?
There was a problem hiding this comment.
Is this related to this change?
Yes, I think it's related -- In the past, a MissingMandatoryValue would not have been raised.
Why not just use the key directly when raising the exception?
Something like this?
raise MissingMandatoryValue(f"Missing mandatory value: {key}")My point was: if we call format_and_raise in OmegaConf.to_container instead of here in BaseContainer._to_content, then the format_and_raise function will not have the correct key passed in to it (since the high-level function does not know what key caused the exception).
There was a problem hiding this comment.
Something like this?
raise MissingMandatoryValue(f"Missing mandatory value: {key}")
Yes.
Depending on exactly what is going on here, you may need to pass the key from the outside or use the key from the offending node (node._key()).
There was a problem hiding this comment.
I tried it in this commit on a separate branch (which is based off of this PR branch).
The tests pass, but we are losing some information in the error messages. For example, here is the error before the commit:
>>> OmegaConf.to_object(OmegaConf.create({"a": {"b": "???"}}))
Traceback (most recent call last):
...
omegaconf.errors.MissingMandatoryValue: Missing mandatory value: b
full_key: a.b
object_type=dictand here is the error after the commit:
>>> OmegaConf.to_object(OmegaConf.create({"a": {"b": "???"}}))
Traceback (most recent call last):
...
omegaconf.errors.MissingMandatoryValue: Missing mandatory value: b
full_key:
object_type=dictThere is no longer information about the full_key that caused the error.
Generally speaking, it's better to format exceptions at high level functions, otherwise you risk formatting them multiple times.
Is it really that bad to format multiple times?
|
|
Unsubscribing for now, please request review when ready. |
| try: | ||
| node = conf._get_node(key, throw_on_missing_value=throw_on_missing) | ||
| except MissingMandatoryValue as e: | ||
| conf._format_and_raise(key=key, value=None, cause=e) |
There was a problem hiding this comment.
Something like this?
raise MissingMandatoryValue(f"Missing mandatory value: {key}")
Yes.
Depending on exactly what is going on here, you may need to pass the key from the outside or use the key from the offending node (node._key()).
| raise ConfigKeyError(f"Missing key {key}") | ||
| elif throw_on_missing_value and value._is_missing(): | ||
| raise MissingMandatoryValue("Missing mandatory value") | ||
| raise MissingMandatoryValue("Missing mandatory value: $KEY") |
There was a problem hiding this comment.
You can try this here:
raise MissingMandatoryValue("Missing mandatory value: '{value._key()}'")
| child_node=lambda cfg: cfg._get_node(0), | ||
| ), | ||
| id="to_container:throw_on_missing,list_item", | ||
| ), |
There was a problem hiding this comment.
This is adding almost 200 lines to an already huge test file.
We should refactor this file somehow at some point.
odelalleau
left a comment
There was a problem hiding this comment.
One minor typo in the doc
Co-authored-by: Olivier Delalleau <507137+odelalleau@users.noreply.github.com>
Co-authored-by: Olivier Delalleau <507137+odelalleau@users.noreply.github.com>
odelalleau
left a comment
There was a problem hiding this comment.
Good on my side, thanks @Jasha10!
This PR introduces a
throw_on_missing keywordarg to theOmegaConf.to_containersignature.MandatoryMissingValueexceptions are raised or suppressed depending on whetherthrow_on_missingis set (closes Feature: throw_on_missing keyword argument for OmegaConf.to_container #501).As discussed in CallingInterpolationToMissingValueErrorexceptions are raised or suppressed depending on whetherthrow_on_missingis set (closes CallingOmegaConf.to_containeron interpolation-to-missing errors #727).OmegaConf.to_containeron interpolation-to-missing errors #727, we will keep the current behavior for raisingInterpolationResolutionError: they are raised if and only ifOmegaConf.to_containeris called withresolve=True.The
OmegaConf.to_objectmethod usesthrow_on_missing==Truewhen calling intoOmegaConf.to_container.This PR replaces #503, which is outdated.
TODO:
OmegaConf.to_containeron interpolation-to-missing errors #727 is resolved)