-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
200 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,6 +106,4 @@ def select_node( | |
): | ||
return default | ||
|
||
if value is not None and value._is_missing(): | ||
return None | ||
return value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
tests/interpolation/built_in_resolvers/test_oc_deprecated.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import re | ||
from typing import Any | ||
|
||
from pytest import mark, param, raises, warns | ||
|
||
from omegaconf import OmegaConf | ||
from omegaconf._utils import _ensure_container | ||
from omegaconf.errors import InterpolationResolutionError | ||
|
||
|
||
@mark.parametrize( | ||
("cfg", "key", "expected_value", "expected_warning"), | ||
[ | ||
param( | ||
{"a": 10, "b": "${oc.deprecated: a}"}, | ||
"b", | ||
10, | ||
"'b' is deprecated. Change your code and config to use 'a'", | ||
id="value", | ||
), | ||
param( | ||
{"a": 10, "b": "${oc.deprecated: a, '$KEY is deprecated'}"}, | ||
"b", | ||
10, | ||
"b is deprecated", | ||
id="value-custom-message", | ||
), | ||
param( | ||
{ | ||
"a": 10, | ||
"b": "${oc.deprecated: a, ${warning}}", | ||
"warning": "$KEY is bad, $NEW_KEY is good", | ||
}, | ||
"b", | ||
10, | ||
"b is bad, a is good", | ||
id="value-custom-message-config-variable", | ||
), | ||
param( | ||
{"a": {"b": 10}, "b": "${oc.deprecated: a}"}, | ||
"b", | ||
OmegaConf.create({"b": 10}), | ||
"'b' is deprecated. Change your code and config to use 'a'", | ||
id="dict", | ||
), | ||
param( | ||
{"a": {"b": 10}, "b": "${oc.deprecated: a}"}, | ||
"b.b", | ||
10, | ||
"'b' is deprecated. Change your code and config to use 'a'", | ||
id="dict_value", | ||
), | ||
param( | ||
{"a": [0, 1], "b": "${oc.deprecated: a}"}, | ||
"b", | ||
OmegaConf.create([0, 1]), | ||
"'b' is deprecated. Change your code and config to use 'a'", | ||
id="list", | ||
), | ||
param( | ||
{"a": [0, 1], "b": "${oc.deprecated: a}"}, | ||
"b[1]", | ||
1, | ||
"'b' is deprecated. Change your code and config to use 'a'", | ||
id="list_value", | ||
), | ||
], | ||
) | ||
def test_deprecated( | ||
cfg: Any, key: str, expected_value: Any, expected_warning: str | ||
) -> None: | ||
cfg = _ensure_container(cfg) | ||
with warns(UserWarning, match=re.escape(expected_warning)): | ||
value = OmegaConf.select(cfg, key) | ||
assert value == expected_value | ||
assert type(value) == type(expected_value) | ||
|
||
|
||
@mark.parametrize( | ||
("cfg", "error"), | ||
[ | ||
param( | ||
{"a": "${oc.deprecated: z}"}, | ||
"ConfigKeyError raised while resolving interpolation: In oc.deprecate resolver at 'a': Key not found: 'z'", | ||
id="target_not_found", | ||
), | ||
param( | ||
{"a": "${oc.deprecated: 111111}"}, | ||
"ValueError raised while resolving interpolation: oc.deprecated:" | ||
" interpolation key type is not a string (int)", | ||
id="invalid_key_type", | ||
), | ||
param( | ||
{"a": "${oc.deprecated: b, 1000}", "b": 10}, | ||
"ValueError raised while resolving interpolation: oc.deprecated:" | ||
" interpolation message type is not a string (int)", | ||
id="invalid_message_type", | ||
), | ||
], | ||
) | ||
def test_deprecated_target_not_found(cfg: Any, error: str) -> None: | ||
cfg = _ensure_container(cfg) | ||
with raises( | ||
InterpolationResolutionError, | ||
match=re.escape(error), | ||
): | ||
cfg.a |