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_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_deepcopy(self, src: Any) -> None:
c1 = OmegaConf.create(src)
c2 = copy.deepcopy(c1)
assert c1 == c2
assert c1.__dict__.keys() == c2.__dict__.keys()
for k in c1.__dict__.keys():
assert c1.__dict__[k] == c2.__dict__[k]
assert id(c1) != id(c2)
if isinstance(c2, ListConfig):
c2.append(1000)
elif isinstance(c2, DictConfig):
c2.num = 42
assert c1 != c2
def test_set_with_invalid_key() -> None:
cfg = OmegaConf.create([1, 2, 3])
with pytest.raises(KeyValidationError):
cfg["foo"] = 4 # type: ignore
def test_insert_throws_not_changing_list() -> None:
c = OmegaConf.create([])
with pytest.raises(ValueError):
c.insert(0, IllegalType())
assert len(c) == 0
assert c == []
def test_create_node_parent_retained_on_create(node: Any) -> None:
cfg1 = OmegaConf.create({"foo": node})
cfg2 = OmegaConf.create({"zonk": cfg1.foo})
assert cfg2 == {"zonk": node}
assert cfg1.foo._get_parent() == cfg1
def test_missing2(self, class_type: str) -> None:
module: Any = import_module(class_type)
cfg = OmegaConf.create(module.MissingTest.Missing2)
assert cfg == {"head": {"next": "???", "value": 1}}
assert OmegaConf.is_missing(cfg.head, "next")
cfg.head.next = module.LinkedList(value=2)
assert cfg == {"head": {"next": {"next": None, "value": 2}, "value": 1}}
def test_value_node_get_full_key() -> None:
cfg = OmegaConf.create({"foo": IntegerNode(value=10)})
assert cfg._get_node("foo")._get_full_key(None) == "foo" # type: ignore
node = IntegerNode(value=10)
assert node._get_full_key(None) == ""
node = IntegerNode(key="foo", value=10)
assert node._get_full_key(None) == "foo"
def test_resolver_cache_2(restore_resolvers: Any) -> None:
"""
Tests that resolver cache is not shared between different OmegaConf objects
"""
OmegaConf.register_resolver("random", lambda _: random.randint(0, 10000000))
c1 = OmegaConf.create(dict(k="${random:_}"))
c2 = OmegaConf.create(dict(k="${random:_}"))
assert c1.k != c2.k
assert c1.k == c1.k
assert c2.k == c2.k
def test_set_value_validation_fail(input_: Any, key: Any, value: Any) -> None:
c = OmegaConf.create(input_)
with raises(ValidationError):
c[key] = value
def test_struct_set_on_nested_dict() -> None:
c = OmegaConf.create(dict(a=dict(b=10)))
OmegaConf.set_struct(c, True)
with pytest.raises(AttributeError):
# noinspection PyStatementEffect
c.foo
assert "a" in c
assert c.a.b == 10
with pytest.raises(AttributeError, match=re.escape("a.foo")):
# noinspection PyStatementEffect
c.a.foo