def test_allow_objects() -> None: c = OmegaConf.create({"foo": AnyNode()}) with raises(UnsupportedValueType): c.foo = IllegalType() c._set_flag("allow_objects", True) iv = IllegalType() c.foo = iv assert c.foo == iv
def test_dict_assign_illegal_value_nested() -> None: c = OmegaConf.create({"a": {}}) iv = IllegalType() with raises(UnsupportedValueType, match=re.escape("key: a.b")): c.a.b = iv with flag_override(c, "allow_objects", True): c.a.b = iv assert c.a.b == iv
def test_merge_allow_objects(merge: Any) -> None: iv = IllegalType() cfg = OmegaConf.create({"a": 10}) with raises(UnsupportedValueType): merge(cfg, {"foo": iv}) cfg._set_flag("allow_objects", True) ret = merge(cfg, {"foo": iv}) assert ret == {"a": 10, "foo": iv}
def test_get_structured_config_data_illegal_value(test_cls: Any) -> None: with raises(UnsupportedValueType): _utils.get_structured_config_data(test_cls, allow_objects=None) with raises(UnsupportedValueType): _utils.get_structured_config_data(test_cls, allow_objects=False) d = _utils.get_structured_config_data(test_cls, allow_objects=True) assert d["x"] == IllegalType()
def test_nested_list_assign_illegal_value() -> None: c = OmegaConf.create({"a": [None]}) with pytest.raises( UnsupportedValueType, match=re.escape( dedent("""\ Value 'IllegalType' is not a supported primitive type full_key: a[0]""")), ): c.a[0] = IllegalType()
def test_insert_throws_not_changing_list() -> None: c = OmegaConf.create([]) iv = IllegalType() with pytest.raises(ValueError): c.insert(0, iv) assert len(c) == 0 assert c == [] with flag_override(c, "allow_objects", True): c.insert(0, iv) assert c == [iv]
def test_append_throws_not_changing_list() -> None: c = OmegaConf.create([]) iv = IllegalType() with pytest.raises(ValueError): c.append(iv) assert len(c) == 0 assert c == [] validate_list_keys(c) with flag_override(c, "allow_objects", True): c.append(iv) assert c == [iv]
def test_allow_objects(self, module: Any) -> None: cfg = OmegaConf.structured(module.Plugin) iv = IllegalType() with raises(UnsupportedValueType): cfg.params = iv cfg = OmegaConf.structured(module.Plugin, flags={"allow_objects": True}) cfg.params = iv assert cfg.params == iv cfg = OmegaConf.structured(module.Plugin) with flag_override(cfg, "allow_objects", True): cfg.params = iv assert cfg.params == iv cfg = OmegaConf.structured({"plugin": module.Plugin}) pwo = module.Plugin(name="foo", params=iv) with raises(UnsupportedValueType): cfg.plugin = pwo with flag_override(cfg, "allow_objects", True): cfg.plugin = pwo assert cfg.plugin == pwo
class _TestDataclassIllegalValue: x: Any = IllegalType()
pytest.param( {"target": "builtins.str", "params": {"object": 43}}, {}, "43", id="builtin_types", ), # passthrough pytest.param( {"target": "tests.AClass"}, {"a": 10, "b": 20, "c": 30}, AClass(a=10, b=20, c=30), id="passthrough", ), pytest.param( {"target": "tests.AClass"}, {"a": 10, "b": 20, "c": 30, "d": {"x": IllegalType()}}, AClass(a=10, b=20, c=30, d={"x": IllegalType()}), id="passthrough", ), ], ) def test_class_instantiate( input_conf: Any, passthrough: Dict[str, Any], expected: Any, ) -> Any: conf = OmegaConf.create(input_conf) assert isinstance(conf, DictConfig) conf_copy = copy.deepcopy(conf) obj = utils.instantiate(conf, **passthrough) assert obj == expected # make sure config is not modified by instantiate assert conf == conf_copy
{ "a": 10, "b": 20, "c": 30 }, AClass(a=10, b=20, c=30), id="passthrough", ), pytest.param( {"_target_": "tests.AClass"}, { "a": 10, "b": 20, "c": 30, "d": { "x": IllegalType() } }, AClass(a=10, b=20, c=30, d={"x": IllegalType()}), id="passthrough", ), pytest.param( UntypedPassthroughConf, {"a": IllegalType()}, UntypedPassthroughClass(a=IllegalType()), id="untyped_passthrough", ), ], ) def test_class_instantiate(input_conf: Any, passthrough: Dict[str, Any], expected: Any) -> Any:
create=lambda: OmegaConf.structured(StructuredWithMissing), op=lambda cfg: OmegaConf.update(cfg, "num", None, merge=True), exception_type=ValidationError, msg="child 'num' is not Optional", parent_node=lambda cfg: cfg, child_node=lambda cfg: cfg._get_node("num"), object_type=StructuredWithMissing, key="num", ), id="structured:update:none_to_non_optional", ), pytest.param( Expected( create=lambda: OmegaConf.create({}), op=lambda cfg: OmegaConf.update( cfg, "a", IllegalType(), merge=True), key="a", exception_type=UnsupportedValueType, msg="Value 'IllegalType' is not a supported primitive type", ), id="dict:update:object_of_illegal_type", ), # pop pytest.param( Expected( create=lambda: create_readonly({"foo": "bar"}), op=lambda cfg: cfg.pop("foo"), key="foo", child_node=lambda cfg: cfg._get_node("foo"), exception_type=ReadonlyConfigError, msg="Cannot pop from read-only node",
{}, "43", id="builtin_types", ), # Check that none is instantiated correctly pytest.param(None, {}, None, id="instantiate_none"), # passthrough pytest.param( {"_target_": "tests.AClass"}, {"a": 10, "b": 20, "c": 30}, AClass(a=10, b=20, c=30), id="passthrough", ), pytest.param( {"_target_": "tests.AClass"}, {"a": 10, "b": 20, "c": 30, "d": {"x": IllegalType()}}, AClass(a=10, b=20, c=30, d={"x": IllegalType()}), id="passthrough", ), pytest.param( {"_target_": "tests.AClass"}, { "a": 10, "b": 20, "c": 30, "d": {"x": [10, IllegalType()]}, }, AClass(a=10, b=20, c=30, d={"x": [10, IllegalType()]}), id="passthrough:list", ), pytest.param(
def test_set_anynode_with_illegal_type() -> None: cfg = OmegaConf.create({"a": 5}) with raises(ValidationError): cfg.a = IllegalType()
def test_create_nested_dict_with_illegal_value() -> None: with raises(ValueError, match=re.escape("key: a.b")): OmegaConf.create({"a": {"b": IllegalType()}})
{ "a": 10, "b": 20, "c": 30 }, AClass(a=10, b=20, c=30), id="passthrough", ), pytest.param( {"target": "tests.AClass"}, { "a": 10, "b": 20, "c": 30, "d": { "x": IllegalType() } }, AClass(a=10, b=20, c=30, d={"x": IllegalType()}), id="passthrough", ), ], ) def test_class_instantiate( input_conf: Any, passthrough: Dict[str, Any], expected: Any, ) -> Any: conf = OmegaConf.create(input_conf) assert isinstance(conf, DictConfig) conf_copy = copy.deepcopy(conf)
(OmegaConf.create({}), {}), (OmegaConf.create([]), []), (OmegaConf.create({"foo": OmegaConf.create([])}), {"foo": []}), (OmegaConf.create([OmegaConf.create({})]), [{}]), (OmegaConf.create({"foo": Path("bar")}), {"foo": Path("bar")}), ], ) def test_create_value(input_: Any, expected: Any) -> None: assert OmegaConf.create(input_) == expected @mark.parametrize( "input_", [ # top level dict {"x": IllegalType()}, {"x": {"y": IllegalType()}}, {"x": [IllegalType()]}, # top level list [IllegalType()], [[IllegalType()]], [{"x": IllegalType()}], [{"x": [IllegalType()]}], ], ) def test_create_allow_objects(input_: Any) -> None: # test creating from a primitive container cfg = OmegaConf.create(input_, flags={"allow_objects": True}) assert cfg == input_ # test creating from an OmegaConf object, inheriting the allow_objects flag
(OmegaConf.create({"foo": OmegaConf.create([])}), { "foo": [] }), (OmegaConf.create([OmegaConf.create({})]), [{}]), ], ) def test_create_value(input_: Any, expected: Any) -> None: assert OmegaConf.create(input_) == expected @mark.parametrize( "input_", [ # top level dict { "x": IllegalType() }, { "x": { "y": IllegalType() } }, { "x": [IllegalType()] }, # top level list [IllegalType()], [[IllegalType()]], [{ "x": IllegalType() }],
def test_create_list_with_illegal_value_idx0() -> None: with raises(UnsupportedValueType, match=re.escape("key: [0]")): OmegaConf.create([IllegalType()])
def test_create_list_with_illegal_value_idx1() -> None: lst = [1, IllegalType(), 3] with raises(UnsupportedValueType, match=re.escape("key: [1]")): OmegaConf.create(lst)
def test_get_type(cfg: Any, type_: Any) -> None: cfg = OmegaConf.create(cfg) assert OmegaConf.get_type(cfg, "foo") == type_ @mark.parametrize( "obj, type_", [ (10, int), (10.0, float), (True, bool), ("foo", str), (DictConfig(content={}), dict), (ListConfig(content=[]), list), (IllegalType, IllegalType), (IllegalType(), IllegalType), ], ) def test_get_type_on_raw(obj: Any, type_: Any) -> None: assert OmegaConf.get_type(obj) == type_ def test_is_issubclass() -> None: cfg = OmegaConf.structured(ConcretePlugin) t = OmegaConf.get_type(cfg) assert t is not None and issubclass(t, ConcretePlugin) @mark.parametrize( ("cfg", "expected"), [
class _TestAttrllegalValue: x: Any = IllegalType()
(BooleanNode, {"foo": "var"}), (BooleanNode, DictConfig({"foo": "var"})), (FloatNode, [1, 2]), (FloatNode, ListConfig([1, 2])), (FloatNode, {"foo": "var"}), (FloatNode, DictConfig({"foo": "var"})), (StringNode, [1, 2]), (StringNode, ListConfig([1, 2])), (StringNode, {"foo": "var"}), (StringNode, b"\xf0\xf1\xf2"), (FloatNode, DictConfig({"foo": "var"})), (AnyNode, [1, 2]), (AnyNode, ListConfig([1, 2])), (AnyNode, {"foo": "var"}), (AnyNode, DictConfig({"foo": "var"})), (AnyNode, IllegalType()), (partial(EnumNode, Color), "Color.TYPO"), (partial(EnumNode, Color), "TYPO"), (partial(EnumNode, Color), Enum1.FOO), (partial(EnumNode, Color), "Enum1.RED"), (partial(EnumNode, Color), 1000000), (partial(EnumNode, Color), 1.0), (partial(EnumNode, Color), b"binary"), (partial(EnumNode, Color), True), (partial(EnumNode, Color), [1, 2]), (partial(EnumNode, Color), {"foo": "bar"}), (partial(EnumNode, Color), ListConfig([1, 2])), (partial(EnumNode, Color), DictConfig({"foo": "bar"})), ], ) def test_invalid_inputs(type_: type, input_: Any) -> None:
{ "a": 10, "b": 20, "c": 30 }, AClass(a=10, b=20, c=30), id="passthrough", ), param( {"_target_": "tests.AClass"}, { "a": 10, "b": 20, "c": 30, "d": { "x": IllegalType() } }, AClass(a=10, b=20, c=30, d={"x": IllegalType()}), id="oc_incompatible_passthrough", ), param( {"_target_": "tests.AClass"}, { "a": 10, "b": 20, "c": 30, "d": { "x": [10, IllegalType()] }, },
def test_create_dict_with_illegal_value() -> None: with raises(UnsupportedValueType, match=re.escape("key: a")): OmegaConf.create({"a": IllegalType()})