omegaconf icon indicating copy to clipboard operation
omegaconf copied to clipboard

```OmegaConf.to_yaml``` adds unexpected new lines and question mark to a long string

Open Waerden001 opened this issue 2 years ago • 1 comments

when creating a yaml file from an omegaconf object using OmegaConf.to_yaml, and if the key string is long, instead of keeping the key string as it is, omegaconf automatically adds a question mark and one or several new lines to the string, I'm wondering if there's a way to disable this behavior?

To reproduce

from omegaconf import OmegaConf
key = "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
value = 1.0

conf = OmegaConf.create({key: value})
yaml_str = OmegaConf.to_yaml(conf)
# yaml_str = '? EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n: 1.0\n'

Waerden001 avatar May 31 '22 02:05 Waerden001

The short answer is "no, OmegaConf does not currently support disabling this behavior".

The longer explanation is that OmegaConf uses pyyaml as a backend for loading and dumping yaml. Adding the question mark is in-fact done by the pyyaml library. You can confirm this by checking that the same behavior occurs with pure pyyaml:

$ python -q
>>> key = "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
>>> value = 1.0
>>> import yaml
>>> conf_py = {key: value}
>>> print(yaml.dump(conf_py))
? EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
: 1.0

Use of the question mark is in-fact valid yaml (per the yaml spec), but I understand that you might want to disable this in some cases. It might be possible to configure the pyyaml dumper to prevent the question mark, in which case perhaps we could expose a keyword argument to allow such configuration:

yaml_str = OmegaConf.to_yaml(conf, pyyaml_config=...)

Jasha10 avatar May 31 '22 12:05 Jasha10