Skip to content

Commit

Permalink
oc.create() does not set the readonly flag anymore
Browse files Browse the repository at this point in the history
The motivation is that otherwise it prevents merging the config with
another one.
  • Loading branch information
odelalleau committed Apr 12, 2021
1 parent a6a983a commit 9d80510
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
6 changes: 1 addition & 5 deletions omegaconf/resolvers/oc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ def create(obj: Any, _parent_: Container) -> Any:
from omegaconf import OmegaConf

assert isinstance(_parent_, BaseContainer)
ret = OmegaConf.create(obj, parent=_parent_)
# Since this node is re-generated on-the-fly, changes would be lost: we mark it
# as read-only to avoid mistakes.
ret._set_flag("readonly", True)
return ret
return OmegaConf.create(obj, parent=_parent_)


def env(key: str, default: Any = _DEFAULT_MARKER_) -> Optional[str]:
Expand Down
21 changes: 13 additions & 8 deletions tests/interpolation/built_in_resolvers/test_create_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from pytest import mark, param, raises

from omegaconf import DictConfig, ListConfig, OmegaConf, flag_override
from omegaconf import DictConfig, ListConfig, OmegaConf
from omegaconf.errors import InterpolationResolutionError


Expand Down Expand Up @@ -54,7 +54,7 @@ def test_create(cfg: Any, key: str, expected: Any) -> None:
val = cfg[key]
assert val == expected
assert type(val) is type(expected)
assert val._get_flag("readonly")
assert val._get_flag("readonly") is None


def test_create_error() -> None:
Expand All @@ -74,14 +74,13 @@ def test_write_into_output() -> None:
}
)
x = cfg.x
assert x._get_flag("readonly")
assert x._get_flag("readonly") is None

# "Force-write" into the node generated by `oc.create`.
with flag_override(x, "readonly", False):
x.a = 1
x.b.c = 2
# Write into the node generated by `oc.create`.
x.a = 1
x.b.c = 2

# The node that we force-wrote into should be modified.
# The node that we wrote into should be modified.
assert x.a == 1
assert x.b.c == 2

Expand Down Expand Up @@ -132,3 +131,9 @@ def test_resolver_output_list_to_listconfig(
assert isinstance(c.x, ListConfig)
assert c.x == expected
assert c.x._parent is c


def test_merge_into_created_node() -> None:
cfg: Any = OmegaConf.create({"x": "${oc.create:{y: 0}}"})
cfg = OmegaConf.merge(cfg, {"x": {"z": 1}})
assert cfg == {"x": {"y": 0, "z": 1}}

0 comments on commit 9d80510

Please sign in to comment.