Skip to main content
Version: 1.0

Defaults List

Β Example (Click Here)

You can define a defaults list in your primary Structured Config just like you can in your primary config.yaml file. The example below extends the previous example by adding a defaults list that will load db=mysql by default.


from omegaconf import MISSING, OmegaConf  # Do not confuse with dataclass.MISSING

@dataclass
class MySQLConfig:
...

@dataclass
class PostGreSQLConfig:
...

defaults = [
# Load the config "mysql" from the config group "db"
{"db": "mysql"}
]

@dataclass
class Config:
# this is unfortunately verbose due to @dataclass limitations
defaults: List[Any] = field(default_factory=lambda: defaults)

# Hydra will populate this field based on the defaults list
db: Any = MISSING

cs = ConfigStore.instance()
cs.store(group="db", name="mysql", node=MySQLConfig)
cs.store(group="db", name="postgresql", node=PostGreSQLConfig)
cs.store(name="config", node=Config)


@hydra.main(config_name="config")
def my_app(cfg: Config) -> None:
print(OmegaConf.to_yaml(cfg))


if __name__ == "__main__":
my_app()

Running my_app.py loads the mysql config option by default:

$ python my_app.py
db:
driver: mysql
...

You can override the default option via the command line:

$ python my_app.py db=postgresql
db:
driver: postgresql
...

Requiring users to specify a default list value​

Set db as MISSING to require the user to specify a value on the command line.

Defaults list with a missing db
defaults = [
{"db": MISSING}
]


Output
$ python my_app.py
You must specify 'db', e.g, db=<OPTION>
Available options:
mysql
postgresql