How to use the omegaconf.OmegaConf.merge function in omegaconf

To help you get started, we’ve selected a few omegaconf examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github omry / omegaconf / tests / structured_conf / test_structured_config.py View on Github external
def validate_frozen_impl(conf: DictConfig) -> None:
    with pytest.raises(ReadonlyConfigError):
        conf.x = 20

    with pytest.raises(ReadonlyConfigError):
        conf.list[0] = 10

    with pytest.raises(ReadonlyConfigError):
        conf.user.age = 20

    # merge into is rejected because it mutates a readonly object
    with pytest.raises(ReadonlyConfigError):
        conf.merge_with({"user": {"name": "iceman"}})

    # Normal merge is allowed.
    ret = OmegaConf.merge(conf, {"user": {"name": "iceman"}})
    assert ret == {"user": {"name": "iceman", "age": 10}, "x": 10, "list": [1, 2, 3]}
    with pytest.raises(ReadonlyConfigError):
        ret.user.age = 20
github omry / omegaconf / tests / test_nodes.py View on Github external
def test_pretty_with_enum() -> None:
    cfg = OmegaConf.create()
    assert isinstance(cfg, DictConfig)
    cfg.foo = EnumNode(Enum1)
    cfg.foo = Enum1.FOO

    expected = """foo: FOO
"""
    s = cfg.pretty()
    assert s == expected
    assert (
        OmegaConf.merge({"foo": EnumNode(Enum1, value="???")}, OmegaConf.create(s))
        == cfg
    )
github omry / omegaconf / tests / structured_conf / test_structured_config.py View on Github external
def test_merge_into_Dict(self, class_type: str) -> None:
        module: Any = import_module(class_type)
        cfg = OmegaConf.structured(module.DictExamples)
        res = OmegaConf.merge(cfg, {"strings": {"x": "abc"}})
        assert res.strings == {"a": "foo", "b": "bar", "x": "abc"}
github omry / omegaconf / tests / test_merge.py View on Github external
def test_merge_no_eq_verify(
    a_: Tuple[int], b_: Tuple[int], expected: Tuple[int]
) -> None:
    a = OmegaConf.create(a_)
    b = OmegaConf.create(b_)
    c = OmegaConf.merge(a, b)
    # verify merge result is expected
    assert expected == c
github omry / omegaconf / tests / test_merge.py View on Github external
def test_with_readonly_c1(c1: Any, c2: Any) -> None:
    cfg1 = OmegaConf.create(c1)
    cfg2 = OmegaConf.create(c2)
    OmegaConf.set_readonly(cfg1, True)
    cfg3 = OmegaConf.merge(cfg1, cfg2)
    assert OmegaConf.is_readonly(cfg3)
github omry / omegaconf / tests / test_interpolation.py View on Github external
def test_merge_with_interpolation() -> None:
    cfg = OmegaConf.create(
        {"foo": 10, "bar": "${foo}", "typed_bar": IntegerNode("${foo}")}
    )

    assert OmegaConf.merge(cfg, {"bar": 20}) == {"foo": 10, "bar": 20, "typed_bar": 10}
    assert OmegaConf.merge(cfg, {"typed_bar": 30}) == {
        "foo": 10,
        "bar": 10,
        "typed_bar": 30,
    }

    with pytest.raises(ValidationError):
        OmegaConf.merge(cfg, {"typed_bar": "nope"})
github omry / omegaconf / tests / test_matrix.py View on Github external
def test_none_assignment_and_merging_in_dict(
        self, node_type: Any, values: Any
    ) -> None:
        values = copy.deepcopy(values)
        for value in values:
            node = node_type(value=value, is_optional=False)
            data = {"node": node}
            cfg = OmegaConf.create(obj=data)
            verify(cfg, "node", none=False, opt=False, missing=False, inter=False)
            msg = "child 'node' is not Optional"
            with pytest.raises(ValidationError, match=re.escape(msg)):
                cfg.node = None

            with pytest.raises(ValidationError):
                OmegaConf.merge(cfg, {"node": None})
github shagunsodhani / torch-template / codes / utils / config.py View on Github external
def _load_components(config: ConfigType) -> ConfigType:
    """Load the different componenets in a config

    Args:
        config (ConfigType)

    Returns:
        ConfigType
    """
    special_key = "_load"
    if config is not None and special_key in config:
        loaded_config = read_config_file(config.pop(special_key))
        updated_config = OmegaConf.merge(loaded_config, config)
        assert isinstance(updated_config, ConfigType)
        return updated_config
    return config
github facebookresearch / hydra / hydra / _internal / config_loader.py View on Github external
msg = "Could not load {}".format(new_cfg)
                    raise MissingConfigException(msg, new_cfg)
                else:
                    options = self.get_group_options(family)
                    if options:
                        msg = "Could not load {}, available options:\n{}:\n\t{}".format(
                            new_cfg, family, "\n\t".join(options)
                        )
                    else:
                        msg = "Could not load {}".format(new_cfg)
                    raise MissingConfigException(msg, new_cfg, options)
            else:
                return cfg

        else:
            return OmegaConf.merge(cfg, loaded_cfg)

        assert False