Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def validate_frozen_impl(conf: DictConfig) -> None:
with pytest.raises(ReadonlyConfigError):
conf.x = 20
with pytest.raises(ReadonlyConfigError):
conf.list[0] = 10
with pytest.raises(ReadonlyConfigError):
conf.user.age = 20
# merge into is rejected because it mutates a readonly object
with pytest.raises(ReadonlyConfigError):
conf.merge_with({"user": {"name": "iceman"}})
# Normal merge is allowed.
ret = OmegaConf.merge(conf, {"user": {"name": "iceman"}})
assert ret == {"user": {"name": "iceman", "age": 10}, "x": 10, "list": [1, 2, 3]}
with pytest.raises(ReadonlyConfigError):
ret.user.age = 20
def test_pretty_with_enum() -> None:
cfg = OmegaConf.create()
assert isinstance(cfg, DictConfig)
cfg.foo = EnumNode(Enum1)
cfg.foo = Enum1.FOO
expected = """foo: FOO
"""
s = cfg.pretty()
assert s == expected
assert (
OmegaConf.merge({"foo": EnumNode(Enum1, value="???")}, OmegaConf.create(s))
== cfg
)
def test_merge_into_Dict(self, class_type: str) -> None:
module: Any = import_module(class_type)
cfg = OmegaConf.structured(module.DictExamples)
res = OmegaConf.merge(cfg, {"strings": {"x": "abc"}})
assert res.strings == {"a": "foo", "b": "bar", "x": "abc"}
def test_merge_no_eq_verify(
a_: Tuple[int], b_: Tuple[int], expected: Tuple[int]
) -> None:
a = OmegaConf.create(a_)
b = OmegaConf.create(b_)
c = OmegaConf.merge(a, b)
# verify merge result is expected
assert expected == c
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_merge_with_interpolation() -> None:
cfg = OmegaConf.create(
{"foo": 10, "bar": "${foo}", "typed_bar": IntegerNode("${foo}")}
)
assert OmegaConf.merge(cfg, {"bar": 20}) == {"foo": 10, "bar": 20, "typed_bar": 10}
assert OmegaConf.merge(cfg, {"typed_bar": 30}) == {
"foo": 10,
"bar": 10,
"typed_bar": 30,
}
with pytest.raises(ValidationError):
OmegaConf.merge(cfg, {"typed_bar": "nope"})
def test_none_assignment_and_merging_in_dict(
self, node_type: Any, values: Any
) -> None:
values = copy.deepcopy(values)
for value in values:
node = node_type(value=value, is_optional=False)
data = {"node": node}
cfg = OmegaConf.create(obj=data)
verify(cfg, "node", none=False, opt=False, missing=False, inter=False)
msg = "child 'node' is not Optional"
with pytest.raises(ValidationError, match=re.escape(msg)):
cfg.node = None
with pytest.raises(ValidationError):
OmegaConf.merge(cfg, {"node": None})
def _load_components(config: ConfigType) -> ConfigType:
"""Load the different componenets in a config
Args:
config (ConfigType)
Returns:
ConfigType
"""
special_key = "_load"
if config is not None and special_key in config:
loaded_config = read_config_file(config.pop(special_key))
updated_config = OmegaConf.merge(loaded_config, config)
assert isinstance(updated_config, ConfigType)
return updated_config
return config
msg = "Could not load {}".format(new_cfg)
raise MissingConfigException(msg, new_cfg)
else:
options = self.get_group_options(family)
if options:
msg = "Could not load {}, available options:\n{}:\n\t{}".format(
new_cfg, family, "\n\t".join(options)
)
else:
msg = "Could not load {}".format(new_cfg)
raise MissingConfigException(msg, new_cfg, options)
else:
return cfg
else:
return OmegaConf.merge(cfg, loaded_cfg)
assert False