omry / omegaconf

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

Resolve relative variables inside of a list #1138

Closed v-iashin closed 1 year ago

v-iashin commented 1 year ago

Is your feature request related to a problem? Please describe.

from omegaconf import OmegaConf

# works
cfg = OmegaConf.create('''
    a: 1
    b: ${a}
    c: [1, 2]
''')
OmegaConf.resolve(cfg)
print(cfg)

# does not work `yaml.parser.ParserError: while parsing a flow sequence`
cfg = OmegaConf.create('''
    a: 1
    b: [${a}, 2]
''')
OmegaConf.resolve(cfg)
print(cfg)

Describe the solution you'd like I'd like the second example to work.

Describe alternatives you've considered None

Additional context None

MatteoVoges commented 1 year ago

Hey, in yaml flow style (using [ ] and { } instead of indentation) the curly braces indicate a dictionary, so yaml can't parse it properly. To use interpolations in a flow sequence, you have to indicate to a string with quoting the interpolation, so the following (edited) example will work:

cfg = OmegaConf.create('''
    a: 1
    b: ["${a}", 2]
''')
OmegaConf.resolve(cfg)
print(cfg)

Hope that helps!

v-iashin commented 1 year ago

Excellent thanks!