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_error_retains_type() -> None:
cfg = OmegaConf.structured(ConcretePlugin)
with pytest.raises(ValidationError):
cfg.merge_with({"params": {"foo": "error"}})
assert OmegaConf.get_type(cfg) == ConcretePlugin
def test_get_type_on_raw(obj: Any, type_: Any) -> None:
assert OmegaConf.get_type(obj) == type_
def test_get_ref_type_with_conflict() -> None:
cfg = OmegaConf.create(
{"user": User, "inter": DictConfig(ref_type=Plugin, content="${user}")}
)
assert OmegaConf.get_type(cfg.user) == User
assert _utils.get_ref_type(cfg.user) == Optional[User]
# Interpolation inherits both type and ref type from the target
assert OmegaConf.get_type(cfg.inter) == User
assert _utils.get_ref_type(cfg.inter) == Optional[User]
def test_is_issubclass() -> None:
cfg = OmegaConf.structured(ConcretePlugin)
t = OmegaConf.get_type(cfg)
assert t is not None and issubclass(t, ConcretePlugin)
def test_plugin_merge(self, class_type: str) -> None:
module: Any = import_module(class_type)
plugin = OmegaConf.structured(module.Plugin)
concrete = OmegaConf.structured(module.ConcretePlugin)
ret = OmegaConf.merge(plugin, concrete)
assert ret == concrete
assert OmegaConf.get_type(ret) == module.ConcretePlugin
more_fields = OmegaConf.structured(module.PluginWithAdditionalField)
ret = OmegaConf.merge(plugin, more_fields)
assert ret == more_fields
assert OmegaConf.get_type(ret) == module.PluginWithAdditionalField
def test_merge(self, class_type: str) -> None:
module: Any = import_module(class_type)
cfg1 = OmegaConf.create({"plugin": module.Plugin})
cfg2 = OmegaConf.create({"plugin": module.ConcretePlugin})
assert cfg2.plugin == module.ConcretePlugin
res: Any = OmegaConf.merge(cfg1, cfg2)
assert OmegaConf.get_type(res.plugin) == module.ConcretePlugin
assert (
OmegaConf.get_type(res.plugin.params)
== module.ConcretePlugin.FoobarParams
)
def test_nested_config_is_none(self, class_type: str) -> None:
module: Any = import_module(class_type)
cfg = OmegaConf.structured(module.NestedWithNone)
assert cfg == {"plugin": None}
assert OmegaConf.get_type(cfg, "plugin") is None
assert _utils.get_ref_type(cfg, "plugin") == Optional[module.Plugin]
if target is None:
return
def is_typed(c: Any) -> bool:
return isinstance(c, DictConfig) and c._metadata.ref_type not in (Any, dict)
if not is_typed(target):
return
# target must be optional by now. no need to check the type of value if None.
if value is None:
return
target_type = target._metadata.ref_type
value_type = OmegaConf.get_type(value)
if is_dict(value_type) and is_dict(target_type):
return
# TODO: simplify this flow. (if assign validate assign else validate merge)
# is assignment illegal?
validation_error = (
target_type is not None
and value_type is not None
and not issubclass(value_type, target_type)
)
if not is_assign:
# merge
# Merging of a dictionary is allowed even if assignment is illegal (merge would do deeper checks)
validation_error = not is_dict(value_type) and validation_error