param(Color, "RED", EnumNode(enum_type=Color, value=Color.RED), id="Color"), param(Color, "Color.RED", EnumNode(enum_type=Color, value=Color.RED), id="Color"), # bad type param(IllegalType, "nope", ValidationError, id="bad_type"), # DictConfig param(dict, {"foo": "bar"}, DictConfig(content={"foo": "bar"}), id="DictConfig"), param(Plugin, Plugin(), DictConfig(content=Plugin()), id="DictConfig[Plugin]"), # ListConfig param(list, [1, 2, 3], ListConfig(content=[1, 2, 3]), id="ListConfig"), ], ) def test_node_wrap(target_type: Any, value: Any, expected: Any) -> None: from omegaconf.omegaconf import _maybe_wrap if isinstance(expected, Node): res = _node_wrap(type_=target_type, key="foo", value=value, is_optional=False, parent=None)
@mark.parametrize("ref_type", [None, Any]) @mark.parametrize("assign", [None, {}, {"foo": "bar"}, [1, 2, 3]]) def test_assign_to_reftype_none_or_any(ref_type: Any, assign: Any) -> None: cfg = OmegaConf.create({"foo": DictConfig(ref_type=ref_type, content={})}) cfg.foo = assign assert cfg.foo == assign @mark.parametrize( "ref_type,assign", [ param(Plugin, None, id="plugin_none"), param(Plugin, Plugin, id="plugin_plugin"), param(Plugin, Plugin(), id="plugin_plugin()"), param(Plugin, ConcretePlugin, id="plugin_concrete"), param(Plugin, ConcretePlugin(), id="plugin_concrete()"), param(ConcretePlugin, None, id="concrete_none"), param(ConcretePlugin, ConcretePlugin, id="subclass=subclass_obj"), param(ConcretePlugin, ConcretePlugin(), id="subclass=subclass_obj"), ], ) class TestAssignAndMergeIntoReftypePlugin: def _test_assign(self, ref_type: Any, value: Any, assign: Any) -> None: cfg = OmegaConf.create( {"foo": DictConfig(ref_type=ref_type, content=value)}) assert _utils.get_type_hint(cfg, "foo") == Optional[ref_type] cfg.foo = assign assert cfg.foo == assign assert _utils.get_type_hint(cfg, "foo") == Optional[ref_type]
@mark.parametrize("ref_type", [None, Any]) @mark.parametrize("assign", [None, {}, {"foo": "bar"}, [1, 2, 3]]) def test_assign_to_reftype_none_or_any(ref_type: Any, assign: Any) -> None: cfg = OmegaConf.create({"foo": DictConfig(ref_type=ref_type, content={})}) cfg.foo = assign assert cfg.foo == assign @mark.parametrize( "ref_type,assign", [ (Plugin, None), (Plugin, Plugin), (Plugin, Plugin()), (Plugin, ConcretePlugin), (Plugin, ConcretePlugin()), (ConcretePlugin, None), param(ConcretePlugin, ConcretePlugin, id="subclass=subclass_obj"), param(ConcretePlugin, ConcretePlugin(), id="subclass=subclass_obj"), ], ) class TestAssignAndMergeIntoReftypePlugin: def _test_assign(self, ref_type: Any, value: Any, assign: Any) -> None: cfg = OmegaConf.create({"foo": DictConfig(ref_type=ref_type, content=value)}) assert _utils.get_ref_type(cfg, "foo") == Optional[ref_type] cfg.foo = assign assert cfg.foo == assign assert _utils.get_ref_type(cfg, "foo") == Optional[ref_type]
id="append_dict[str,str]_to_dict[str,int]", ), param( ListConfig([], element_type=Dict[str, int]), None, {"key2": "foo"}, "Value 'foo' of type 'str' could not be converted to Integer", id="append_dict[str,str]_to_dict[str,int]", ), param( ListConfig([{ "key2": ConcretePlugin() }], element_type=Dict[str, ConcretePlugin]), 0, {"key": Plugin()}, "Invalid type assigned: Plugin is not a subclass of ConcretePlugin", id="append_dict[str,str]_to_dict[str,int]", ), param( ListConfig([[ConcretePlugin()]], element_type=List[ConcretePlugin]), 0, [Plugin()], "Invalid type assigned: Plugin is not a subclass of ConcretePlugin", id="append_dict[str,str]_to_dict[str,int]", ), param( ListConfig([], element_type=Dict[str, ConcretePlugin]), None, {"key": Plugin()},
Dict[Any, Any], {"foo": "bar"}, DictConfig(content={"foo": "bar"}), id="DictConfig", ), param(List[Any], {"foo": "bar"}, ValidationError, id="dict_to_list"), param(List[int], {"foo": "bar"}, ValidationError, id="dict_to_list[int]"), param( Any, {"foo": "bar"}, DictConfig(content={"foo": "bar"}), id="dict_to_any", ), param(Plugin, {"foo": "bar"}, ValidationError, id="dict_to_plugin"), # Structured Config param(Plugin, Plugin(), DictConfig(content=Plugin()), id="DictConfig[Plugin]"), param(Any, Plugin(), DictConfig(content=Plugin()), id="plugin_to_any"), param( Dict[str, int], Plugin(), ValidationError, id="plugin_to_dict[str, int]", ), param(List[Any], Plugin(), ValidationError, id="plugin_to_list"), param(List[int], Plugin(), ValidationError, id="plugin_to_list[int]"), # ListConfig param(List[Any], [1, 2, 3], ListConfig(content=[1, 2, 3]), id="ListConfig"), param(Dict[Any, Any], [1, 2, 3], ValidationError, id="list_to_dict"), param(Dict[str, int], [1, 2, 3], ValidationError, id="list_to_dict[str-int]"), param(Any, [1, 2, 3], ListConfig(content=[1, 2, 3]), id="list_to_any"), ],
param(Color, "foo", ValidationError, id="Color"), param(Color, True, ValidationError, id="Color"), param(Color, 1, EnumNode(enum_type=Color, value=Color.RED), id="Color"), param(Color, 1.0, ValidationError, id="Color"), param(Color, Color.RED, EnumNode(enum_type=Color, value=Color.RED), id="Color"), param(Color, "RED", EnumNode(enum_type=Color, value=Color.RED), id="Color"), param( Color, "Color.RED", EnumNode(enum_type=Color, value=Color.RED), id="Color" ), # bad type param(IllegalType, "nope", ValidationError, id="bad_type"), # DictConfig param( dict, {"foo": "bar"}, DictConfig(content={"foo": "bar"}), id="DictConfig" ), param(Plugin, Plugin(), DictConfig(content=Plugin()), id="DictConfig[Plugin]"), # ListConfig param(list, [1, 2, 3], ListConfig(content=[1, 2, 3]), id="ListConfig"), ], ) def test_node_wrap(target_type: Any, value: Any, expected: Any) -> None: from omegaconf.omegaconf import _maybe_wrap if isinstance(expected, Node): res = _node_wrap( type_=target_type, key="foo", value=value, is_optional=False, parent=None ) assert type(res) == type(expected) assert res == expected assert res._key() == "foo" else: