Search code examples
omegaconf

OmegaConf get interpolation node as string literal (eithout evaluating it)


With OmegaConf, is it possible to "turn off" node interpolation, so that when you access an interpolation node it just returns the literal string, i.e. "${resolver:value}" instead of evaluating the interpolation?

I have a program that uses configuration files with some custom interpolations, but I sometimes want to evaluate or compare the configs programatically without worrying about the interpolations (they might correspond to environment variables that aren't set, for example).


Solution

  • You can access the raw value using internal APIs:

    c = OmegaConf.create({"a" :10, "b": "${a}"})
    v = c._get_node("b")._value()
    print(type(v), v)
    # <class 'str'> ${a}
    

    Comparing configs works correctly in the case you are describing (Or in general when some custom resolvers are not resolving):

    c1 = OmegaConf.create({"e": "${oc.env:FOO}", "a": 1})
    c2 = OmegaConf.create({"e": "${oc.env:FOO}", "a": 1})
    print(c1 == c2)
    # True
    c2.a = 2
    print(c1 == c2)
    # False