omry / omegaconf

Flexible Python configuration system. The last one you will ever need.
BSD 3-Clause "New" or "Revised" License
1.91k stars 104 forks source link

Differences between `to_object()` and `resolve()` #1092

Closed MatteoVoges closed 1 year ago

MatteoVoges commented 1 year ago

Describe the bug When I define a custom resolver with a new interpolation in it, it seems like the resolving behavior differs, depending which function I use. If this is intended, please explain why.

To Reproduce Resolver:

def example(key):
  return {"first": f"${{{key}.a}}", "second": f"{{{key}.b}}"}

OmegaConf.register_new_resolver("example", example)

I load some yaml in, containing the resolver:

key:
  a: 1
  b: 2
test: ${example:key}

Now if I resolve the config with OmegaConf.resolve() I get omegaconf.errors.UnsupportedValueType: Value 'dict' is not a supported primitive type.

With OmegaConf.to_object() I get the EXPECTED result:

key:
  a: 1
  b: 2
test:
  first: ${key.a}
  second: ${key.b}

If resolve would just do that, it would be fine, because then I can simply resolve twice. My goal result is:

key:
  a: 1
  b: 2
test:
  first: 1
  second: 2

which I can achieve at the moment with OmegaConf.to_object(OmegaConf.create(OmegaConf.to_object(config))), but this is no good solution because of the unneccessary loading.

Expected behavior I would expect, that the functions behave the same-

Additional context

Jasha10 commented 1 year ago

Looks like a bug to me.

Edit: I've created PR #1093 to address this.

MatteoVoges commented 1 year ago

Thanks @Jasha10 for the quick fix. Everything works as expected now!