def test_readonly_from_cli() -> None: c = OmegaConf.create({"foo": {"bar": [1]}}) assert isinstance(c, DictConfig) OmegaConf.set_readonly(c, True) cli = OmegaConf.from_dotlist(["foo.bar=[2]"]) cfg2 = OmegaConf.merge(c, cli) assert OmegaConf.is_readonly(c) assert OmegaConf.is_readonly(cfg2)
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 shallow_update(default: DictLike, update: DictLike, copy=False): if default is None or update is None: return if isinstance(default, DictConfig): if (ro := OmegaConf.is_readonly(default)): default = default.copy() OmegaConf.set_readonly(default, False) default.update(update) OmegaConf.set_readonly(default, ro)
def test_readonly_flag(src): 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_create_flags_overriding(input_: Any) -> Any: cfg = OmegaConf.create(input_) OmegaConf.set_struct(cfg, True) # by default flags are inherited cfg2 = OmegaConf.create(cfg) assert OmegaConf.is_struct(cfg2) assert not OmegaConf.is_readonly(cfg2) # but specified flags are replacing all of the flags (even those that are not specified) cfg2 = OmegaConf.create(cfg, flags={"readonly": True}) assert not OmegaConf.is_struct(cfg2) assert OmegaConf.is_readonly(cfg2)
def test_merge_with_c2_readonly(c1: Any, c2: Any, expected: Any) -> None: a = OmegaConf.create(c1) b = OmegaConf.create(c2) OmegaConf.set_readonly(b, True) a.merge_with(b) assert a == expected assert OmegaConf.is_readonly(a)
def test_readonly_nested_list(): c = OmegaConf.create([[1]]) assert not OmegaConf.is_readonly(c) assert not OmegaConf.is_readonly(c[0]) OmegaConf.set_readonly(c, True) assert OmegaConf.is_readonly(c) assert OmegaConf.is_readonly(c[0]) OmegaConf.set_readonly(c, False) assert not OmegaConf.is_readonly(c) assert not OmegaConf.is_readonly(c[0]) OmegaConf.set_readonly(c, None) assert not OmegaConf.is_readonly(c) assert not OmegaConf.is_readonly(c[0]) OmegaConf.set_readonly(c[0], True) assert not OmegaConf.is_readonly(c) assert OmegaConf.is_readonly(c[0])
def test_readonly_nested_list() -> None: c = OmegaConf.create([[1]]) assert isinstance(c, ListConfig) assert not OmegaConf.is_readonly(c) assert not OmegaConf.is_readonly(c[0]) OmegaConf.set_readonly(c, True) assert OmegaConf.is_readonly(c) assert OmegaConf.is_readonly(c[0]) OmegaConf.set_readonly(c, False) assert not OmegaConf.is_readonly(c) assert not OmegaConf.is_readonly(c[0]) OmegaConf.set_readonly(c, None) assert not OmegaConf.is_readonly(c) assert not OmegaConf.is_readonly(c[0]) OmegaConf.set_readonly(c[0], True) assert not OmegaConf.is_readonly(c) assert OmegaConf.is_readonly(c[0])
def test_with_readonly_c2(merge: Any, c1: Any, c2: Any) -> None: cfg1 = OmegaConf.create(c1) cfg2 = OmegaConf.create(c1) OmegaConf.set_readonly(cfg2, True) cfg3 = merge(cfg1, cfg2) assert OmegaConf.is_readonly(cfg3)
def test_with_readonly(c1: Any, c2: Any) -> None: cfg = OmegaConf.create(c1) OmegaConf.set_readonly(cfg, True) cfg2 = OmegaConf.merge(cfg, c2) assert OmegaConf.is_readonly(cfg2)