-
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
1 parent
abc60ae
commit f5fb0cc
Showing
7 changed files
with
183 additions
and
21 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
The new built-in resolver `oc.create` can be used to dynamically generate config nodes |
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
134 changes: 134 additions & 0 deletions
134
tests/interpolation/built_in_resolvers/test_create_resolver.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,134 @@ | ||
from typing import Any, Dict, List | ||
|
||
from pytest import mark, param, raises | ||
|
||
from omegaconf import DictConfig, ListConfig, OmegaConf, flag_override | ||
from omegaconf.errors import InterpolationResolutionError | ||
|
||
|
||
@mark.parametrize( | ||
("cfg", "key", "expected"), | ||
[ | ||
# Note that since `oc.create` is simply calling `OmegaConf.create`, which is | ||
# already thoroughly tested, we do not do extensive tests here. | ||
param( | ||
{"x": "${oc.create:{a: 0, b: 1}}"}, | ||
"x", | ||
OmegaConf.create({"a": 0, "b": 1}), | ||
id="dict", | ||
), | ||
param( | ||
{"x": "${oc.create:[0, 1, 2]}"}, | ||
"x", | ||
OmegaConf.create([0, 1, 2]), | ||
id="list", | ||
), | ||
param( | ||
{"x": "${oc.create:{a: 0, b: ${y}}}", "y": 5}, | ||
"x", | ||
OmegaConf.create({"a": 0, "b": 5}), | ||
id="dict:interpolated_value", | ||
), | ||
param( | ||
{"x": "${oc.create:[0, 1, ${y}]}", "y": 5}, | ||
"x", | ||
OmegaConf.create([0, 1, 5]), | ||
id="list:interpolated_value", | ||
), | ||
param( | ||
{"x": "${oc.create:${y}}", "y": {"a": 0}}, | ||
"x", | ||
OmegaConf.create({"a": 0}), | ||
id="dict:interpolated_node", | ||
), | ||
param( | ||
{"x": "${oc.create:${y}}", "y": [0, 1]}, | ||
"x", | ||
OmegaConf.create([0, 1]), | ||
id="list:interpolated_node", | ||
), | ||
], | ||
) | ||
def test_create(cfg: Any, key: str, expected: Any) -> None: | ||
cfg = OmegaConf.create(cfg) | ||
val = cfg[key] | ||
assert val == expected | ||
assert type(val) is type(expected) | ||
assert val._get_flag("readonly") | ||
|
||
|
||
def test_create_error() -> None: | ||
cfg = OmegaConf.create({"x": "${oc.create:0}"}) | ||
with raises(InterpolationResolutionError, match="ValidationError"): | ||
cfg.x | ||
|
||
|
||
def test_write_into_output() -> None: | ||
cfg = OmegaConf.create( | ||
{ | ||
"x": "${oc.create:${y}}", | ||
"y": { | ||
"a": 0, | ||
"b": {"c": 1}, | ||
}, | ||
} | ||
) | ||
x = cfg.x | ||
assert x._get_flag("readonly") | ||
|
||
# "Force-write" into the node generated by `oc.create`. | ||
with flag_override(x, "readonly", False): | ||
x.a = 1 | ||
x.b.c = 2 | ||
|
||
# The node that we force-wrote into should be modified. | ||
assert x.a == 1 | ||
assert x.b.c == 2 | ||
|
||
# The interpolated node should not be modified. | ||
assert cfg.y.a == 0 | ||
assert cfg.y.b.c == 1 | ||
|
||
# Re-accessing the node "forgets" the changes. | ||
assert cfg.x.a == 0 | ||
assert cfg.x.b.c == 1 | ||
|
||
|
||
@mark.parametrize( | ||
("cfg", "expected"), | ||
[ | ||
({"a": 0, "b": 1}, {"a": 0, "b": 1}), | ||
({"a": "${y}"}, {"a": -1}), | ||
({"a": 0, "b": "${x.a}"}, {"a": 0, "b": 0}), | ||
({"a": 0, "b": "${.a}"}, {"a": 0, "b": 0}), | ||
({"a": "${..y}"}, {"a": -1}), | ||
], | ||
) | ||
def test_resolver_output_dict_to_dictconfig( | ||
restore_resolvers: Any, cfg: Dict[str, Any], expected: Dict[str, Any] | ||
) -> None: | ||
OmegaConf.register_new_resolver("dict", lambda: cfg) | ||
c = OmegaConf.create({"x": "${oc.create:${dict:}}", "y": -1}) | ||
assert isinstance(c.x, DictConfig) | ||
assert c.x == expected | ||
assert c.x._parent is c | ||
|
||
|
||
@mark.parametrize( | ||
("cfg", "expected"), | ||
[ | ||
([0, 1], [0, 1]), | ||
(["${y}"], [-1]), | ||
([0, "${x.0}"], [0, 0]), | ||
([0, "${.0}"], [0, 0]), | ||
(["${..y}"], [-1]), | ||
], | ||
) | ||
def test_resolver_output_list_to_listconfig( | ||
restore_resolvers: Any, cfg: List[Any], expected: List[Any] | ||
) -> None: | ||
OmegaConf.register_new_resolver("list", lambda: cfg) | ||
c = OmegaConf.create({"x": "${oc.create:${list:}}", "y": -1}) | ||
assert isinstance(c.x, ListConfig) | ||
assert c.x == expected | ||
assert c.x._parent is c |
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