Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_readonly_flag(src: Union[Dict[str, Any], List[Any]]) -> None:
c = OmegaConf.create(src)
assert not OmegaConf.is_readonly(c)
OmegaConf.set_readonly(c, True)
assert OmegaConf.is_readonly(c)
OmegaConf.set_readonly(c, False)
assert not OmegaConf.is_readonly(c)
OmegaConf.set_readonly(c, None)
assert not OmegaConf.is_readonly(c)
def create_readonly(cfg: Any) -> Any:
cfg = OmegaConf.create(cfg)
OmegaConf.set_readonly(cfg, True)
return cfg
def test_readonly_list_del() -> None:
c = OmegaConf.create([1, 2, 3])
assert isinstance(c, ListConfig)
OmegaConf.set_readonly(c, True)
with raises(ReadonlyConfigError, match="[1]"):
del c[1]
assert c == [1, 2, 3]
def test_with_readonly_c1(c1: Any, c2: Any) -> None:
cfg1 = OmegaConf.create(c1)
cfg2 = OmegaConf.create(c2)
OmegaConf.set_readonly(cfg1, True)
cfg3 = OmegaConf.merge(cfg1, cfg2)
assert OmegaConf.is_readonly(cfg3)
def test_readonly_list_change_item() -> None:
c = OmegaConf.create([1, 2, 3])
assert isinstance(c, ListConfig)
OmegaConf.set_readonly(c, True)
with raises(ReadonlyConfigError, match="[1]"):
c[1] = 10
assert c == [1, 2, 3]
def test_readonly_flag(src: Union[Dict[str, Any], List[Any]]) -> None:
c = OmegaConf.create(src)
assert not OmegaConf.is_readonly(c)
OmegaConf.set_readonly(c, True)
assert OmegaConf.is_readonly(c)
OmegaConf.set_readonly(c, False)
assert not OmegaConf.is_readonly(c)
OmegaConf.set_readonly(c, None)
assert not OmegaConf.is_readonly(c)
def test_dict_merge_readonly_into_readwrite(c1: Any, c2: Any, expected: Any) -> None:
c1 = OmegaConf.create(c1)
c2 = OmegaConf.create(c2)
OmegaConf.set_readonly(c2.node, True)
with pytest.raises(ReadonlyConfigError):
c2.node.foo = 10
assert OmegaConf.merge(c1, c2) == expected
c1.merge_with(c2)
assert c1 == expected
def test_with_readonly_c2(c1: Any, c2: Any) -> None:
cfg1 = OmegaConf.create(c1)
cfg2 = OmegaConf.create(c1)
OmegaConf.set_readonly(cfg2, True)
cfg3 = OmegaConf.merge(cfg1, cfg2)
assert OmegaConf.is_readonly(cfg3)
def set_config(self, cfg):
try:
OmegaConf.set_readonly(self, False)
self.hydra = copy.deepcopy(cfg.hydra)
finally:
OmegaConf.set_readonly(self, True)
def make_config_immutable(config: ConfigType) -> ConfigType:
"""Set the config to be immutable
Args:
config (ConfigType):
Returns:
ConfigType:
"""
OmegaConf.set_readonly(config, True)
return config