Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix handling of throw_on_missing flag for select() on ListConfig #564

Merged
merged 4 commits into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/563.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Calling `OmegaConf.select()` on a missing node from a ListConfig with `throw_on_missing` set to True now raises the intended exception.
21 changes: 10 additions & 11 deletions omegaconf/omegaconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,17 +979,6 @@ def _select_one(
if isinstance(c, DictConfig):
assert isinstance(ret_key, str)
val = c._get_node(ret_key, validate_access=False)
if val is not None:
assert isinstance(val, Node)
if val._is_missing():
if throw_on_missing:
raise MissingMandatoryValue(
f"Missing mandatory value : {c._get_full_key(ret_key)}"
)
else:
return val, ret_key
else:
val = None
elif isinstance(c, ListConfig):
assert isinstance(ret_key, str)
if not is_int(ret_key):
Expand All @@ -1008,5 +997,15 @@ def _select_one(
else:
assert False

if val is not None:
assert isinstance(val, Node)
if val._is_missing():
if throw_on_missing:
raise MissingMandatoryValue(
f"Missing mandatory value : {c._get_full_key(ret_key)}"
)
else:
return val, ret_key

assert val is None or isinstance(val, Node)
return val, ret_key
4 changes: 3 additions & 1 deletion tests/test_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def test_select_key_from_empty(struct: Optional[bool]) -> None:
pytest.param([], "0", None, id="list:oob"),
pytest.param([1, "2"], "0", 1, id="list:int"),
pytest.param([1, "2"], "1", "2", id="list:str"),
pytest.param(["???"], "0", None, id="list:missing"),
pytest.param([1, {"a": 10, "c": ["foo", "bar"]}], "0", 1),
pytest.param([1, {"a": 10, "c": ["foo", "bar"]}], "1.a", 10),
pytest.param([1, {"a": 10, "c": ["foo", "bar"]}], "1.b", None),
Expand Down Expand Up @@ -82,7 +83,8 @@ def test_select_default(
@pytest.mark.parametrize(
"cfg, key",
[
pytest.param({"missing": "???"}, "missing", id="missing"),
pytest.param({"missing": "???"}, "missing", id="missing_dict"),
pytest.param(["???"], "0", id="missing_list"),
],
)
def test_select_default_throw_on_missing(
Expand Down