Skip to content

Commit

Permalink
Make OmegaConf.select() consistent with cfg.get() regarding inter…
Browse files Browse the repository at this point in the history
…polations to non-existing nodes
  • Loading branch information
odelalleau committed Feb 26, 2021
1 parent 381387c commit c87648e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion news/565.api_change
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Container methods `get()` and `pop()` do not return their default value anymore when the accessed key is an interpolation that cannot be resolved: instead, an exception is raised.
`OmegaConf.select()` as well as container methods `get()` and `pop()` do not return their default value anymore when the accessed key is an interpolation that cannot be resolved: instead, an exception is raised.

2 changes: 1 addition & 1 deletion omegaconf/omegaconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ def select(
throw_on_missing=throw_on_missing,
throw_on_resolution_failure=throw_on_resolution_failure,
)
except (ConfigKeyError, InterpolationKeyError):
except ConfigKeyError:
if default is not _EMPTY_MARKER_:
return default
else:
Expand Down
16 changes: 15 additions & 1 deletion tests/test_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from omegaconf import MissingMandatoryValue, OmegaConf
from omegaconf._utils import _ensure_container
from omegaconf.errors import InterpolationKeyError


@pytest.mark.parametrize("struct", [True, False, None])
Expand Down Expand Up @@ -64,7 +65,6 @@ def test_select(
[
pytest.param({}, "not_found", id="empty"),
pytest.param({"missing": "???"}, "missing", id="missing"),
pytest.param({"inter": "${bad_key}"}, "inter", id="inter_bad_key"),
],
)
def test_select_default(
Expand Down Expand Up @@ -101,6 +101,20 @@ def test_select_default_throw_on_missing(
OmegaConf.select(cfg, key, default=default, throw_on_missing=True)


@pytest.mark.parametrize(
["cfg", "key"],
[
pytest.param({"inter": "${bad_key}"}, "inter", id="inter_bad_key"),
],
)
def test_select_default_throw_on_resolution_failure(cfg: Any, key: Any) -> None:
cfg = OmegaConf.create(cfg)

# throw on resolution failure still throws if default is provided
with pytest.raises(InterpolationKeyError):
OmegaConf.select(cfg, key, default=123, throw_on_resolution_failure=True)


@pytest.mark.parametrize(
("cfg", "node_key", "expected"),
[
Expand Down

0 comments on commit c87648e

Please sign in to comment.