omry / omegaconf

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

How can I resolve within a list? #1027

Closed famura closed 1 year ago

famura commented 1 year ago

Is your feature request related to a problem? Please describe. I guess this is more of a beginner's question that might lead to a feature request.

Describe the solution you'd like I would like to write a config yaml that resolves multiple things within one list for one item, e.g.

foo:
  bar: [1, ${custom_resolver:thing_to_resolve1}, 2, 3, ${custom_resolver:thing_to_resolve1}]
  ...

You can assume that

foo:
  bar: ${custom_resolver:thing_to_resolve1}
  ...

already works.

Describe alternatives you've considered None, yet. Maybe writing a custom resolver, but I really don't know if that is necessary.

Jasha10 commented 1 year ago

Hi @famura, The issue here is with the pyyaml library that OmegaConf uses as a backend. The problem is that the example you gave is not valid yaml.

Either of the following should work:

Option 1: Quoting the value to be resolved:

foo:
  bar: [1, "${custom_resolver:thing_to_resolve1}", 2, 3, "${custom_resolver:thing_to_resolve1}"]

Option 2: Putting each list item on a new line

foo:
  bar:
    - 1
    - ${custom_resolver:thing_to_resolve1}
    - 2
    - 3
    - ${custom_resolver:thing_to_resolve1}
famura commented 1 year ago

Thank you @Jasha10 for your super quick answer. So, the problem is once again on the user side 😅 In fact, these are my first experiences with yaml and I am surprised to that writing the list in a different way solves the problem (your solution 2). Anyhow, many thanks! I consider this issues to be solved 👍