hydra: [Bug] Custom Resolver Only Works Somtimes

πŸ› Bug

Description

This may be an OmegaConf issue, I’m not sure where is the more appropriate place to put it.

I have a custom OmegaConf resolver registered that uses eval because sometimes I need to pass real python objects in a generic way (e.g. I can’t assume what the type will be in code but the writer of the config file knows):

OmegaConf.register_resolver("eval", lambda x: eval(x))

This resolver only seems to work sometimes, and I’m not sure exactly what the rule is. I have a bunch of examples, some work and some don’t work. One notable example that doesnt work is a tuple:

test_fails_tuple: ${eval:(1,2,3)}

will be a string containing β€œ${eval:(1,2,3)}” when I read the value from the DictConfig.

Checklist

  • I checked on the latest version of Hydra
  • I created a minimal repro

To reproduce

** Minimal Code/Config snippet to reproduce **

Config:

test_fails_tuple: ${eval:(1,2,3)}
test_fails_tuple_2: ${eval:tuple([1,2,3])}
test_fails_list: ${eval:[1,2,3]}
test_works_function: ${eval:time.time}
test_works_int: ${eval:42}

Code:

import hydra
from omegaconf import OmegaConf
import time


@hydra.main(config_name="minimal_test.yaml")
def main(cfg):
    OmegaConf.register_resolver("eval", lambda x: eval(x))

    print(cfg.test_fails_tuple)
    print(cfg.test_fails_tuple_2)
    print(cfg.test_fails_list)
    print(cfg.test_works_function())
    print(cfg.test_works_int)

if __name__ == "__main__":
    main()

** Stack trace/error message ** Results of above print messages:

${eval:(1,2,3)}
${eval:tuple([1,2,3])}
${eval:[1,2,3]}
1600444490.9921494
42

Expected Behavior

No matter what python expression I put in the eval handler, it gets passed to eval and returned.

System information

  • Hydra Version : Git master
  • Python version : 3.7.7
  • Virtual environment type and version : Conda 4.5.8
  • Operating system : Ubuntu 18.04

Additional context

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 25 (11 by maintainers)

Most upvoted comments

OmegaConf 2.1 is adding experimental support for allowing objects in configs (omry/omegaconf#382). Note that this voids your warranty, serialization and other things will likely break. This is also experimental, especially before OmegaConf 2.1 is released.

You can try it out from OmegaConf master branch or from https://pypi.org/project/omegaconf/2.1.0.dev3/