I'm looking for a simple use-case of hydra (currently using ver. 1.0.7, but can upgrade as well) but I couldn't figure it out after reading the tutorial. Here is the scenario:
Suppose I store my configs in a @dataclass decorated class as a schema, and I want to
- keep the default values that's set in the python class
- validate a loaded config yaml file against this class (and therefore all classes that recursively make up this
dataclass) - give priority to the values in the loaded yaml file
e.g., I have a yaml file unrelated.yaml with content
title: My app
width: 1024
height: 768
with a python file my_app.py with content
from dataclasses import dataclass
import hydra
from hydra.core.config_store import ConfigStore
from omegaconf import OmegaConf
@dataclass
class DBConfig:
host: str = "localhost"
port: int = 3306
cs = ConfigStore.instance()
cs.store(name="config", node=DBConfig)
@hydra.main(config_name="config")
def my_app(cfg: MyConfig) -> None:
print(OmegaConf.to_yaml(cfg))
if __name__ == "__main__":
my_app()
If I run the python directly, I will get the default values:
$ python my_app.py
host: localhost
port: 3306
but if I run the program with the unrelated yaml file, I will get
$ python my_app.py --config-name unrelated.yaml
title: My app
width: 1024
height: 768
in which case (1) the default values specified are all lost, (2) the config object is loaded with completely unrelated content and (3) no error is thrown here.
I would like to have a solution where I can load a yaml file, validate it against a defined schema, filling in unspecified values with the default ones, and throw error if anything else is found. Is there an easy way to achieve this with hydra, so that I can enjoy other hydra benefits like the simple command line overriding arguments?
This is supported starting at Hydra 1.1.
This this pattern for an example. This is documented in more details in the last page of the Structured Configs tutorial.
Based on your comments you are using an earlier version of Hydra. In the past, Hydra matched the name of the config (schmea) in the ConfigStore to the name of the config file automatically. This does not work when the names of the schema does not match the name of the config as it is in your example.