コード例 #1
0
def test_interpolation_readonly_node() -> None:
    cfg = OmegaConf.structured(User(name="7", age=II("name")))
    resolved = dereference_node(cfg, "age")
    assert resolved == 7
    # The `resolved` node must be read-only because `age` is an integer, so the
    # interpolation cannot return directly the `name` node.
    assert resolved._get_flag("readonly")
コード例 #2
0
def test_resolver_output_list_to_listconfig(restore_resolvers: Any,
                                            cfg: List[Any],
                                            expected: List[Any]) -> None:
    OmegaConf.register_new_resolver("list", lambda: cfg)
    c = OmegaConf.create({"x": "${list:}", "y": -1})
    assert isinstance(c.x, ListConfig)
    assert c.x == expected
    assert dereference_node(c, "x")._get_flag("readonly")
コード例 #3
0
def test_resolver_output_dict(restore_resolvers: Any, readonly: bool) -> None:
    some_dict = {"a": 0, "b": "${y}"}
    OmegaConf.register_new_resolver("dict", lambda: some_dict)
    c = OmegaConf.create({"x": "${dict:}", "y": -1})
    OmegaConf.set_readonly(c, readonly)
    assert isinstance(c.x, dict)
    assert c.x == some_dict
    x_node = dereference_node(c, "x")
    assert isinstance(x_node, InterpolationResultNode)
    assert x_node._get_flag("readonly")
コード例 #4
0
def test_interpolation_like_result_is_not_an_interpolation(
    node_type: Any, value: str, expected: str
) -> None:
    cfg = OmegaConf.create({"x": node_type(value), "y1": "{foo}", "y2": "{foo"})
    assert cfg.x == expected

    # Check that the resulting node is not considered to be an interpolation.
    resolved_node = dereference_node(cfg, "x")
    assert not resolved_node._is_interpolation()

    # Check that the resulting node is read-only.
    with raises(ReadonlyConfigError):
        resolved_node._set_value("foo")
コード例 #5
0
def test_resolver_output_plain_dict_list(restore_resolvers: Any,
                                         readonly: bool, data: Any,
                                         expected_type: type) -> None:
    OmegaConf.register_new_resolver("get_data", lambda: data)
    c = OmegaConf.create({"x": "${get_data:}", "y": -1})
    OmegaConf.set_readonly(c, readonly)

    assert isinstance(c.x, expected_type)
    assert c.x == data

    x_node = dereference_node(c, "x")
    assert isinstance(x_node, InterpolationResultNode)
    assert x_node._get_flag("readonly")