omegaconf icon indicating copy to clipboard operation
omegaconf copied to clipboard

_promote is erasing values from the config

Open gwenzek opened this issue 3 years ago • 0 comments

Describe the bug If you _promote a dict config to a dataclass with some default values, the default values from the dataclass will override the ones from the config.

To Reproduce

Modify test_promote_to_dataclass to add a default value to bar field. And an assertion that x.bar isn't modified during promotion.

    def test_promote_to_dataclass(self, module: Any) -> None:
        @dataclasses.dataclass
        class Foo:
            foo: pathlib.Path
            bar: str = "goodbye.err"
            qub: int = 5

        x = DictConfig({"foo": "hello.txt", "bar": "hello.txt"})
        assert isinstance(x.foo, str)
        assert isinstance(x.bar, str)

        x._promote(Foo)
        assert isinstance(x.foo, pathlib.Path)
        assert x.bar == "hello.txt"
        assert x.qub == 5

This tests currently fails with:

>       assert x.bar == "hello.txt"
E       AssertionError: assert 'goodbye.err' == 'hello.txt'
E         - hello.txt
E         + goodbye.err

This happens because in _promote the code is merging the proto with default values into the original config, while it should be the other way around: merge the config into the proto. This is a simple fix, but also a breaking change in a way.

Additional context

  • OmegaConf version: 2.1.1.dev (pathlib branch)
  • Python version: 3.9.5
  • Operating system: linux

gwenzek avatar Apr 14 '22 12:04 gwenzek