Skip to content

Commit

Permalink
to_container of string interpolation with readonly parent
Browse files Browse the repository at this point in the history
  • Loading branch information
omry committed Jun 20, 2020
1 parent 70bd144 commit e80bce8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions news/275.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix OmegaConf.to_container() failing in some cases when the config is read-only
5 changes: 4 additions & 1 deletion omegaconf/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ class ValueNode(Node):
_val: Any

def __init__(self, parent: Optional[Container], value: Any, metadata: Metadata):
from omegaconf import read_write

super().__init__(parent=parent, metadata=metadata)
self._set_value(value)
with read_write(self):
self._set_value(value)

def _value(self) -> Any:
return self._val
Expand Down
9 changes: 9 additions & 0 deletions tests/test_base_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ def test_to_container(src: Any, expected: Any, expected_with_resolve: Any) -> No
assert container == expected_with_resolve


def test_string_interpolation_with_readonly_parent() -> None:
cfg = OmegaConf.create({"a": 10, "b": {"c": "hello_${a}"}})
OmegaConf.set_readonly(cfg, True)
assert OmegaConf.to_container(cfg, resolve=True) == {
"a": 10,
"b": {"c": "hello_10"},
}


@pytest.mark.parametrize( # type: ignore
"src,expected",
[
Expand Down
2 changes: 1 addition & 1 deletion tests/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
StringNode,
ValueNode,
)
from omegaconf.errors import ValidationError
from omegaconf.errors import ReadonlyConfigError, ValidationError

from . import Color

Expand Down

0 comments on commit e80bce8

Please sign in to comment.