コード例 #1
0
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)
コード例 #2
0
ファイル: test_utils.py プロジェクト: gwenzek/omegaconf
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)
コード例 #3
0
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)
コード例 #4
0
ファイル: test_utils.py プロジェクト: gwenzek/omegaconf
 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,
コード例 #5
0
ファイル: test_utils.py プロジェクト: 7iW/omegaconf
 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,
コード例 #6
0
    "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(
コード例 #7
0
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",
    [
        ([], [], []),