Skip to content

Commit

Permalink
Fix #433 bug where ListConfig is modified after assignment of wrong v…
Browse files Browse the repository at this point in the history
…alue. (#434)
  • Loading branch information
pereman2 authored Nov 9, 2020
1 parent 72ed2ba commit 7b6e645
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions news/433.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug where assignment of an invalid value to a ListConfig raised an exception but left the object modified.
10 changes: 10 additions & 0 deletions omegaconf/listconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,16 @@ def __contains__(self, item: Any) -> bool:
return False

def _set_value(self, value: Any, flags: Optional[Dict[str, bool]] = None) -> None:
try:
previous_content = self.__dict__["_content"]
self._set_value_impl(value, flags)
except Exception as e:
self.__dict__["_content"] = previous_content
raise e

def _set_value_impl(
self, value: Any, flags: Optional[Dict[str, bool]] = None
) -> None:
from omegaconf import OmegaConf, flag_override

if flags is None:
Expand Down
1 change: 1 addition & 0 deletions tests/structured_conf/test_structured_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@ def test_assign_wrong_type_to_list(self, class_type: str, value: Any) -> None:
cfg.list = value
with pytest.raises(ValidationError):
cfg.tuple = value
assert cfg == OmegaConf.structured(module.ListClass)

@pytest.mark.parametrize( # type: ignore
"value",
Expand Down

0 comments on commit 7b6e645

Please sign in to comment.