Append new fields to primary YAML config by default #2457
-
Say I have a YAML defining the main config # configs/config.yaml
foo: bar
baz: 1 And I want the app to add a new, top-level field to the composed config (as the default behavior). So far I have # app.py
from dataclasses import dataclass
import hydra
from hydra.core.config_store import ConfigStore
from omegaconf import OmegaConf
@dataclass
class Extras:
hello: str = 'world'
cs = ConfigStore.instance()
cs.store(group='extras', name='example', package='_global_', node=Extras)
@hydra.main(config_name='config', config_path='configs')
def main(cfg):
resolved_cfg = OmegaConf.to_container(cfg, resolve=True, enum_to_str=True)
print(OmegaConf.to_yaml(resolved_cfg))
if __name__ == '__main__':
main() This accomplishes the goal if I add an override for the
But without the override, the Approach 1: specified the group and ran into the relative path error explained by @Jasha10 here
Approach 2: so I try a leading slash to make the group absolute
Approach 3: add specification for the global package, same error
Am I just running into a fundamental limitation - namely that the only way to adjust my defaults is editing the YAML directly, or is there a more clever solution to this that I'm missing? If all else fails, I suppose I can always hack |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I think passing an override will be the easiest way go, which means hacking Another idea is to use a different config as top-level and pull in # app2.py
from dataclasses import dataclass
import hydra
from hydra.core.config_store import ConfigStore
from omegaconf import OmegaConf
@dataclass
class Extras:
hello: str = "world"
cs = ConfigStore.instance()
cs.store(group="extras", name="example", package="_global_", node=Extras)
top_level_config = {
"defaults": [
"config", # this pulls in `configs/config.yaml`
{"extras": "example"},
]
}
cs.store(name="top_level_config", node=top_level_config)
@hydra.main(config_name="top_level_config", config_path="configs", version_base=None)
def main(cfg):
resolved_cfg = OmegaConf.to_container(cfg, resolve=True, enum_to_str=True)
print(OmegaConf.to_yaml(resolved_cfg))
if __name__ == "__main__":
main() $ python app2.py
foo: bar
baz: 1
hello: world Calling defaults:
- config
- extras: example I think there might some edge cases where the |
Beta Was this translation helpful? Give feedback.
I think passing an override will be the easiest way go, which means hacking
sys.argv
or using the theoverrides
keyword tocompose
.Another idea is to use a different config as top-level and pull in
configs/config.yaml
as a subconfig: