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_omegaconf_create() -> None:
assert OmegaConf.create([]) == []
assert OmegaConf.create({}) == {}
with raises(ValidationError):
assert OmegaConf.create(10) # type: ignore
([{"user": User}, {"user": Group}], pytest.raises(ValidationError)),
(
[{"user": DictConfig(ref_type=User, content=User)}, {"user": Group}],
pytest.raises(ValidationError),
),
([Plugin, ConcretePlugin], ConcretePlugin),
pytest.param(
[{"user": "???"}, {"user": Group}],
{"user": Group},
id="merge_into_missing_node",
),
# missing DictConfig
pytest.param(
[{"dict": DictConfig(content="???")}, {"dict": {"foo": "bar"}}],
{"dict": {"foo": "bar"}},
id="merge_into_missing_DictConfig",
),
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 test_merged_with_nons_subclass(self, class_type: str) -> None:
module: Any = import_module(class_type)
c1 = OmegaConf.structured(module.Plugin)
c2 = OmegaConf.structured(module.FaultyPlugin)
with pytest.raises(ValidationError):
OmegaConf.merge(c1, c2)
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_assignment_of_non_subclass_2(self, class_type: str, rhs: Any) -> None:
module: Any = import_module(class_type)
cfg = OmegaConf.create({"plugin": module.Plugin})
with pytest.raises(ValidationError):
cfg.plugin = rhs
def test_modifiers() -> None:
conf: Modifiers = OmegaConf.structured(Modifiers)
# regular fields cannot take None
with pytest.raises(ValidationError):
conf.num = None # type: ignore
# but Optional fields can
conf.optional_num = None
assert conf.optional_num is None
# Accessing a missing field will trigger MissingMandatoryValue exception
with pytest.raises(MissingMandatoryValue):
# noinspection PyStatementEffect
conf.another_num
# but you can access it once it's been assigned
conf.another_num = 42
assert conf.another_num == 42
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"})
lambda: pytest.raises(ValidationError),
),
def test_integer_1():
c = OmegaConf.create()
c.foo = types.Integer(10)
assert c.foo == 10
with pytest.raises(ValidationError):
c.foo = "string"