Skip to content

Commit

Permalink
Read-only list can no longer be replaced with command line override
Browse files Browse the repository at this point in the history
  • Loading branch information
omry committed Nov 9, 2019
1 parent adde0c4 commit e6262b7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions news/39.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Read-only list can no longer be replaced with command line override
8 changes: 7 additions & 1 deletion omegaconf/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ def get_full_key(self, key):
break
elif isinstance(parent, ListConfig):
if child is None:
full_key = "[{}]".format(key)
if key == "":
full_key = key
else:
full_key = "[{}]".format(key)
else:
for idx, v in enumerate(parent):
if id(v) == id(child):
Expand Down Expand Up @@ -415,6 +418,9 @@ def merge_with(self, *others):
if isinstance(self, DictConfig) and isinstance(other, DictConfig):
Config._map_merge(self, other)
elif isinstance(self, ListConfig) and isinstance(other, ListConfig):
if self._get_flag("readonly"):
raise ReadonlyConfigError(self.get_full_key(""))

self.__dict__["content"] = copy.deepcopy(other.content)
else:
raise TypeError("Merging DictConfig with ListConfig is not supported")
Expand Down
8 changes: 8 additions & 0 deletions tests/test_readonly.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,11 @@ def test_readonly_list_sort():
with pytest.raises(ReadonlyConfigError):
c.sort()
assert c == [3, 1, 2]


def test_readonly_from_cli():
c = OmegaConf.create({"foo": {"bar": [1]}})
OmegaConf.set_readonly(c, True)
cli = OmegaConf.from_dotlist(["foo.bar=[2]"])
with pytest.raises(ReadonlyConfigError, match="foo.bar"):
OmegaConf.merge(c, cli)

0 comments on commit e6262b7

Please sign in to comment.