def test_re_parent() -> None: def validate(cfg1: DictConfig) -> None: assert cfg1._get_parent() is None assert cfg1._get_node("str")._get_parent() == cfg1 # type: ignore assert cfg1._get_node("list")._get_parent() == cfg1 # type: ignore assert cfg1.list._get_node(0)._get_parent() == cfg1.list unode1 = cfg1._get_node("union") assert unode1._get_parent() == cfg1 # type: ignore assert unode1._value()._get_parent() == unode1 # type: ignore cfg = OmegaConf.create({}) assert isinstance(cfg, DictConfig) cfg.str = StringNode("str") cfg.list = [1] cfg.union = UnionNode(123, Union[int, str]) validate(cfg) cfg._get_node("str")._set_parent(None) # type: ignore cfg._get_node("list")._set_parent(None) # type: ignore cfg.list._get_node(0)._set_parent(None) # type:ignore unode = cfg._get_node("union") unode._set_parent(None) # type: ignore unode._value()._set_parent(None) # type: ignore # noinspection PyProtectedMember cfg._re_parent() validate(cfg)
def test_re_parent() -> None: def validate(cfg1: DictConfig) -> None: assert cfg1._get_parent() is None assert cfg1._get_node("str")._get_parent() == cfg1 # type: ignore assert cfg1._get_node("list")._get_parent() == cfg1 # type: ignore assert cfg1.list._get_node(0)._get_parent() == cfg1.list cfg = OmegaConf.create({}) assert isinstance(cfg, DictConfig) cfg.str = StringNode("str") cfg.list = [1] validate(cfg) cfg._get_node("str")._set_parent(None) # type: ignore cfg._get_node("list")._set_parent(None) # type: ignore cfg.list._get_node(0)._set_parent(None) # type:ignore # noinspection PyProtectedMember cfg._re_parent() validate(cfg)
def test_re_parent() -> None: def validate(cfg1: DictConfig) -> None: assert cfg1._get_parent() is None assert cfg1.get_node("str")._get_parent() == cfg1 assert cfg1.get_node("list")._get_parent() == cfg1 assert cfg1.list.get_node(0)._get_parent() == cfg1.list cfg = OmegaConf.create({}) assert isinstance(cfg, DictConfig) cfg.str = StringNode("str") cfg.list = [1] validate(cfg) cfg.get_node("str")._set_parent(None) cfg.get_node("list")._set_parent(None) cfg.list.get_node(0)._set_parent(None) # type: ignore _utils._re_parent(cfg) validate(cfg)
param(float, 1, FloatNode(1), id="float"), param(float, 1.0, FloatNode(1.0), id="float"), param(float, Color.RED, ValidationError, id="float"), # bool param(bool, "foo", ValidationError, id="bool"), param(bool, True, BooleanNode(True), id="bool"), param(bool, 1, BooleanNode(True), id="bool"), param(bool, 0, BooleanNode(False), id="bool"), param(bool, 1.0, ValidationError, id="bool"), param(bool, Color.RED, ValidationError, id="bool"), param(bool, "true", BooleanNode(True), id="bool"), param(bool, "false", BooleanNode(False), id="bool"), param(bool, "on", BooleanNode(True), id="bool"), param(bool, "off", BooleanNode(False), id="bool"), # str param(str, "foo", StringNode("foo"), id="str"), param(str, True, StringNode("True"), id="str"), param(str, 1, StringNode("1"), id="str"), param(str, 1.0, StringNode("1.0"), id="str"), param(str, Color.RED, StringNode("Color.RED"), id="str"), # Color 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,
"input_, index, value, expected, expected_node_type, expectation", [ (["a", "b", "c"], 1, 100, ["a", 100, "b", "c"], AnyNode, None), ( ["a", "b", "c"], 1, IntegerNode(100), ["a", 100, "b", "c"], IntegerNode, None, ), (["a", "b", "c"], 1, "foo", ["a", "foo", "b", "c"], AnyNode, None), ( ["a", "b", "c"], 1, StringNode("foo"), ["a", "foo", "b", "c"], StringNode, None, ), ( ListConfig(element_type=int, content=[]), 0, "foo", None, None, ValidationError, ), ], ) def test_insert(
def test_getattr() -> None: c = OmegaConf.create(["a", "b", "c"]) assert getattr(c, "0") == "a" assert getattr(c, "1") == "b" assert getattr(c, "2") == "c" with pytest.raises(AttributeError): getattr(c, "anything") @pytest.mark.parametrize( # type: ignore "input_, index, value, expected, expected_node_type", [ (["a", "b", "c"], 1, 100, ["a", 100, "b", "c"], AnyNode), (["a", "b", "c"], 1, IntegerNode(100), ["a", 100, "b", "c"], IntegerNode), (["a", "b", "c"], 1, "foo", ["a", "foo", "b", "c"], AnyNode), (["a", "b", "c"], 1, StringNode("foo"), ["a", "foo", "b", "c"], StringNode), ], ) def test_insert( input_: List[str], index: int, value: Any, expected: Any, expected_node_type: type ) -> None: c = OmegaConf.create(input_) c.insert(index, value) assert c == expected assert type(c.get_node(index)) == expected_node_type @pytest.mark.parametrize( # type: ignore "src, append, result", [ ([], [], []),