omry / omegaconf

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

Resolve relative variables inside of a list #1138

Closed v-iashin closed 8 months ago

v-iashin commented 8 months 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 8 months 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 8 months ago

Excellent thanks!