Ejemplo n.º 1
0
 param(({
     "b": {
         "c": 1
     }
 }, {
     "b": 1
 }), {"b": 1}, id="dict:merge_int_dict"),
 param(({
     "list": [1, 2, 3]
 }, {
     "list": [4, 5, 6]
 }), {"list": [4, 5, 6]}),
 param(({
     "a": 1
 }, {
     "a": IntegerNode(10)
 }), {"a": 10}),
 param(({
     "a": 1
 }, {
     "a": IntegerNode(10)
 }), {"a": IntegerNode(10)}),
 param(({
     "a": IntegerNode(10)
 }, {
     "a": 1
 }), {"a": 1}),
 param(({
     "a": IntegerNode(10)
 }, {
     "a": 1
Ejemplo n.º 2
0
@mark.parametrize(
    "target_type, value, expected",
    [
        # Any
        param(Any, "foo", AnyNode("foo"), id="any"),
        param(Any, True, AnyNode(True), id="any"),
        param(Any, 1, AnyNode(1), id="any"),
        param(Any, 1.0, AnyNode(1.0), id="any"),
        param(Any, Color.RED, AnyNode(Color.RED), id="any"),
        param(Any, {}, DictConfig(content={}), id="any_as_dict"),
        param(Any, [], ListConfig(content=[]), id="any_as_list"),
        # int
        param(int, "foo", ValidationError, id="int"),
        param(int, True, ValidationError, id="int"),
        param(int, 1, IntegerNode(1), id="int"),
        param(int, 1.0, ValidationError, id="int"),
        param(int, Color.RED, ValidationError, id="int"),
        # float
        param(float, "foo", ValidationError, id="float"),
        param(float, True, ValidationError, id="float"),
        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"),
Ejemplo n.º 3
0

def validate_list_keys(c: Any) -> None:
    # validate keys are maintained
    for i in range(len(c)):
        assert c._get_node(i)._metadata.key == i


@pytest.mark.parametrize(
    "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=[]),
Ejemplo n.º 4
0
@mark.parametrize(
    "target_type, value, expected",
    [
        # Any
        param(Any, "foo", AnyNode("foo"), id="any"),
        param(Any, True, AnyNode(True), id="any"),
        param(Any, 1, AnyNode(1), id="any"),
        param(Any, 1.0, AnyNode(1.0), id="any"),
        param(Any, Color.RED, AnyNode(Color.RED), id="any"),
        param(Any, {}, DictConfig(content={}), id="any_as_dict"),
        param(Any, [], ListConfig(content=[]), id="any_as_list"),
        # int
        param(int, "foo", ValidationError, id="int"),
        param(int, True, ValidationError, id="int"),
        param(int, 1, IntegerNode(1), id="int"),
        param(int, 1.0, ValidationError, id="int"),
        param(int, Color.RED, ValidationError, id="int"),
        # float
        param(float, "foo", ValidationError, id="float"),
        param(float, True, ValidationError, id="float"),
        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"),
Ejemplo n.º 5
0
 pytest.param([{"a": 1}, {"b": 2}], {"a": 1, "b": 2}, id="dict"),
 pytest.param(
     [
         {"a": {"a1": 1, "a2": 2}},
         {"a": {"a1": 2}},
     ],
     {"a": {"a1": 2, "a2": 2}},
     id="dict",
 ),
 pytest.param([{"a": 1, "b": 2}, {"b": 3}], {"a": 1, "b": 3}, id="dict"),
 pytest.param(
     ({"a": 1}, {"a": {"b": 3}}), {"a": {"b": 3}}, id="dict:merge_dict_into_int"
 ),
 pytest.param(({"b": {"c": 1}}, {"b": 1}), {"b": 1}, id="dict:merge_int_dict"),
 pytest.param(({"list": [1, 2, 3]}, {"list": [4, 5, 6]}), {"list": [4, 5, 6]}),
 pytest.param(({"a": 1}, {"a": IntegerNode(10)}), {"a": 10}),
 pytest.param(({"a": 1}, {"a": IntegerNode(10)}), {"a": IntegerNode(10)}),
 pytest.param(({"a": IntegerNode(10)}, {"a": 1}), {"a": 1}),
 pytest.param(({"a": IntegerNode(10)}, {"a": 1}), {"a": IntegerNode(1)}),
 pytest.param(
     ({"a": "???"}, {"a": {}}), {"a": {}}, id="dict_merge_into_missing"
 ),
 pytest.param(
     ({"a": "???"}, {"a": {"b": 10}}),
     {"a": {"b": 10}},
     id="dict_merge_into_missing",
 ),
 pytest.param(
     ({"a": {"b": 10}}, {"a": "???"}),
     {"a": {"b": 10}},
     id="dict_merge_missing_onto",
Ejemplo n.º 6
0
 [
     # Any
     param(Any, "foo", AnyNode("foo"), id="any"),
     param(Any, b"binary", AnyNode(b"binary"), id="any"),
     param(Any, True, AnyNode(True), id="any"),
     param(Any, 1, AnyNode(1), id="any"),
     param(Any, 1.0, AnyNode(1.0), id="any"),
     param(Any, b"123", AnyNode(b"123"), id="any"),
     param(Any, Color.RED, AnyNode(Color.RED), id="any"),
     param(Any, {}, DictConfig(content={}), id="any_as_dict"),
     param(Any, [], ListConfig(content=[]), id="any_as_list"),
     # int
     param(int, "foo", ValidationError, id="int"),
     param(int, b"binary", ValidationError, id="int"),
     param(int, True, ValidationError, id="int"),
     param(int, 1, IntegerNode(1), id="int"),
     param(int, 1.0, ValidationError, id="int"),
     param(int, Color.RED, ValidationError, id="int"),
     param(int, b"123", ValidationError, id="int"),
     # float
     param(float, "foo", ValidationError, id="float"),
     param(float, b"binary", ValidationError, id="float"),
     param(float, True, ValidationError, id="float"),
     param(float, 1, FloatNode(1), id="float"),
     param(float, 1.0, FloatNode(1.0), id="float"),
     param(float, Color.RED, ValidationError, id="float"),
     param(float, b"123", ValidationError, id="float"),
     # bool
     param(bool, "foo", ValidationError, id="bool"),
     param(bool, b"binary", ValidationError, id="bool"),
     param(bool, True, BooleanNode(True), id="bool"),
Ejemplo n.º 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",
Ejemplo n.º 8
0
 param(Any, "foo", AnyNode("foo"), id="any"),
 param(Any, b"binary", AnyNode(b"binary"), id="any"),
 param(Any, Path("hello.txt"), AnyNode(Path("hello.txt")), id="any"),
 param(Any, True, AnyNode(True), id="any"),
 param(Any, 1, AnyNode(1), id="any"),
 param(Any, 1.0, AnyNode(1.0), id="any"),
 param(Any, b"123", AnyNode(b"123"), id="any"),
 param(Any, Color.RED, AnyNode(Color.RED), id="any"),
 param(Any, {}, DictConfig(content={}), id="any_as_dict"),
 param(Any, [], ListConfig(content=[]), id="any_as_list"),
 # int
 param(int, "foo", ValidationError, id="int"),
 param(int, b"binary", ValidationError, id="int"),
 param(int, Path("hello.txt"), ValidationError, id="int"),
 param(int, True, ValidationError, id="int"),
 param(int, 1, IntegerNode(1), id="int"),
 param(int, 1.0, ValidationError, id="int"),
 param(int, Color.RED, ValidationError, id="int"),
 param(int, b"123", ValidationError, id="int"),
 # float
 param(float, "foo", ValidationError, id="float"),
 param(float, b"binary", ValidationError, id="float"),
 param(float, Path("hello.txt"), ValidationError, id="float"),
 param(float, True, ValidationError, id="float"),
 param(float, 1, FloatNode(1), id="float"),
 param(float, 1.0, FloatNode(1.0), id="float"),
 param(float, Color.RED, ValidationError, id="float"),
 param(float, b"123", ValidationError, id="float"),
 # bool
 param(bool, "foo", ValidationError, id="bool"),
 param(bool, b"binary", ValidationError, id="bool"),
Ejemplo n.º 9
0
 param([{"a": 1}, {"b": 2}], {"a": 1, "b": 2}, id="dict"),
 param(
     [
         {"a": {"a1": 1, "a2": 2}},
         {"a": {"a1": 2}},
     ],
     {"a": {"a1": 2, "a2": 2}},
     id="dict",
 ),
 param([{"a": 1, "b": 2}, {"b": 3}], {"a": 1, "b": 3}, id="dict"),
 param(
     ({"a": 1}, {"a": {"b": 3}}), {"a": {"b": 3}}, id="dict:merge_dict_into_int"
 ),
 param(({"b": {"c": 1}}, {"b": 1}), {"b": 1}, id="dict:merge_int_dict"),
 param(({"list": [1, 2, 3]}, {"list": [4, 5, 6]}), {"list": [4, 5, 6]}),
 param(({"a": 1}, {"a": IntegerNode(10)}), {"a": 10}),
 param(({"a": 1}, {"a": IntegerNode(10)}), {"a": IntegerNode(10)}),
 param(({"a": IntegerNode(10)}, {"a": 1}), {"a": 1}),
 param(({"a": IntegerNode(10)}, {"a": 1}), {"a": IntegerNode(1)}),
 param(
     ({"a": 1.0}, {"a": UnionNode(10.1, Union[float, bool])}),
     {"a": 10.1},
     id="dict_merge_union_into_float",
 ),
 param(
     ({"a": "abc"}, {"a": UnionNode(10.1, Union[float, bool])}),
     {"a": 10.1},
     id="dict_merge_union_into_str",
 ),
 param(
     ({"a": FloatNode(1.0)}, {"a": UnionNode(10.1, Union[float, bool])}),