Skip to main content
Version: 1.0

Config Groups


Structured Configs can be used to implement config groups. Special care needs to be taken when specifying a default value for fields populated by a config group. We will look at why below.

Defining a config group for database
@dataclassclass MySQLConfig:    driver: str = "mysql"    host: str = "localhost"    port: int = 3306
@dataclassclass PostGreSQLConfig:    driver: str = "postgresql"    host: str = "localhost"    port: int = 5432    timeout: int = 10
@dataclassclass Config:    # Keep db omegaconf.MISSING. We will populate it using composition.    db: Any = MISSING
# Create config group `db` with options 'mysql' and 'postgreqsl'cs = ConfigStore.instance()"config", node=Config)"db", name="mysql", node=MySQLConfig)"db", name="postgresql", node=PostGreSQLConfig)
@hydra.main(config_name="config")def my_app(cfg: Config) -> None:    print(OmegaConf.to_yaml(cfg))

The Config class is NOT the Defaults list. We will see the Defaults list in the next page.

You can select the database from the command line:

$ python +db=postgresqldb:  driver: postgresql  host: localhost  password: drowssap  port: 5432  timeout: 10  user: postgres_user

The + above is required because there is no default choice for the config group db. The next page will reintroduce the Defaults List, eliminating the need for the +.

Config inheritance#

Standard Python inheritance can be used to get improved type safety, and to move common fields to the parent class.

Defining a config group for database using inheritance
@dataclassclass DBConfig:    host: str = "localhost"    port: int = MISSING    driver: str = MISSING
@dataclassclass MySQLConfig(DBConfig):    driver: str = "mysql"    port: int = 3306
@dataclassclass PostGreSQLConfig(DBConfig):    driver: str = "postgresql"    port: int = 5432    timeout: int = 10
@dataclassclass Config:    # We can now annotate db as DBConfig which    # improves both static and dynamic type safety.    db: DBConfig = MISSING