def test_merge_nested_list_promotion() -> None: dc1 = DictConfig({"key": [Plugin]}, element_type=List[Plugin]) dc2 = DictConfig({"key": [ConcretePlugin]}) cfg = OmegaConf.merge(dc1, dc2) check_subnode( cfg.key, key=0, value=ConcretePlugin(), type_hint=Plugin, key_type=Any, elt_type=Any, obj_type=ConcretePlugin, )
assert is_list_annotation(type_=type_) == expected @mark.parametrize( "obj, expected", [ # Unwrapped values param(10, Any, id="int"), param(10.0, Any, id="float"), param(True, Any, id="bool"), param("bar", Any, id="str"), param(None, Any, id="NoneType"), param({}, Any, id="dict"), param([], Any, id="List[Any]"), param(tuple(), Any, id="List[Any]"), param(ConcretePlugin(), Any, id="ConcretePlugin"), param(ConcretePlugin, Any, id="ConcretePlugin"), # Optional value nodes param(IntegerNode(10), Optional[int], id="IntegerNode"), param(FloatNode(10.0), Optional[float], id="FloatNode"), param(BooleanNode(True), Optional[bool], id="BooleanNode"), param(StringNode("bar"), Optional[str], id="StringNode"), param( EnumNode(enum_type=Color, value=Color.RED), Optional[Color], id="EnumNode[Color]", ), # Non-optional value nodes: param(IntegerNode(10, is_optional=False), int, id="IntegerNode"), param(FloatNode(10.0, is_optional=False), float, id="FloatNode"), param(BooleanNode(True, is_optional=False), bool, id="BooleanNode"),
Expected( create=lambda: None, op=lambda cfg: OmegaConf.structured(IllegalType), exception_type=ValidationError, msg= "Input class 'IllegalType' is not a structured config. did you forget to decorate it as a dataclass?", object_type_str=None, ref_type_str=None, ), id="dict_create_from_illegal_type", ), param( Expected( create=lambda: None, op=lambda _: OmegaConf.structured( ConcretePlugin(params=ConcretePlugin.FoobarParams(foo="x") ) # type: ignore ), exception_type=ValidationError, msg="Value 'x' could not be converted to Integer", key="foo", full_key="foo", parent_node=lambda _: {}, object_type=ConcretePlugin.FoobarParams, ), id="structured:create_with_invalid_value", ), param( Expected( create=lambda: None, op=lambda cfg: OmegaConf.structured(IllegalType()), exception_type=ValidationError,
@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] def _test_merge(self, ref_type: Any, value: Any, assign: Any) -> 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", [ (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] def _test_merge(self, ref_type: Any, value: Any, assign: Any) -> None: cfg = OmegaConf.create({"foo": DictConfig(ref_type=ref_type, content=value)})
assert OmegaConf.is_missing(cfg, "list") @mark.parametrize( "cfg, expected", [ (None, False), ({}, False), ([], False), ("aa", False), (10, False), (True, False), (bool, False), (StringNode("foo"), False), (ConcretePlugin, False), (ConcretePlugin(), False), (OmegaConf.create({}), True), (OmegaConf.create([]), True), (OmegaConf.structured(ConcretePlugin), True), (OmegaConf.structured(ConcretePlugin()), True), ], ) def test_is_config(cfg: Any, expected: bool) -> None: assert OmegaConf.is_config(cfg) == expected @mark.parametrize( "cfg, expected", [ (None, False), ({}, False),
}, "baz": { "qux": None } }, element_type=Dict[str, Optional[int]], ), Dict[str, Optional[int]], str, Optional[int], dict, id="dict-dict-optional", ), param( DictConfig({"foo": { "bar": ConcretePlugin() }}, element_type=Dict[str, Plugin]), Dict[str, Plugin], str, Plugin, dict, id="dict-of-plugin", ), param( DictConfig({"foo": [ConcretePlugin()]}, element_type=List[Plugin]), List[Plugin], int, Plugin, list, id="list-of-plugin",
assert OmegaConf.is_missing(cfg, "list") @pytest.mark.parametrize( "cfg, expected", [ (None, False), ({}, False), ([], False), ("aa", False), (10, False), (True, False), (bool, False), (StringNode("foo"), False), (ConcretePlugin, False), (ConcretePlugin(), False), (OmegaConf.create({}), True), (OmegaConf.create([]), True), (OmegaConf.structured(ConcretePlugin), True), (OmegaConf.structured(ConcretePlugin()), True), ], ) def test_is_config(cfg: Any, expected: bool) -> None: assert OmegaConf.is_config(cfg) == expected @pytest.mark.parametrize( "cfg, expected", [ (None, False), ({}, False),