Skip to content

Commit

Permalink
Fixed assigment of container to itself (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
omry authored Dec 20, 2020
2 parents e6d53e0 + 92fcb39 commit 1fa65fa
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions news/449.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix assignment of a Container to itself causing it to clear it's content
3 changes: 3 additions & 0 deletions omegaconf/dictconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,9 @@ def _set_value_impl(
) -> None:
from omegaconf import OmegaConf, flag_override

if id(self) == id(value):
return

if flags is None:
flags = {}

Expand Down
3 changes: 3 additions & 0 deletions omegaconf/listconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,9 @@ def _set_value_impl(
) -> None:
from omegaconf import OmegaConf, flag_override

if id(self) == id(value):
return

if flags is None:
flags = {}

Expand Down
13 changes: 13 additions & 0 deletions tests/test_basic_ops_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,3 +758,16 @@ def test_setdefault() -> None:
assert cfg["foo"] == 10

assert cfg["foo"] == 10


@pytest.mark.parametrize( # type: ignore
"c",
[
pytest.param({"a": ListConfig([1, 2, 3], ref_type=list)}, id="list_value"),
pytest.param({"a": DictConfig({"b": 10}, ref_type=dict)}, id="dict_value"),
],
)
def test_self_assign_list_value_with_ref_type(c: Any) -> None:
cfg = OmegaConf.create(c)
cfg.a = cfg.a
assert cfg == c

0 comments on commit 1fa65fa

Please sign in to comment.