fb-hydraomegaconf

How to set up hydra config to accept a custom enum?


How do I set up a my hydra config to accept a custom enum? Specifically I followed the Structured Config Schema tutorial.

I have a dataclass config:

@dataclass_validate
@dataclass
class CustomConfig:
    custom_enum: CustomEnum

With the custom enum:

class CustomEnum(str, Enum):
    ENUM1 = "enum1"
    ENUM2 = "enum2"

Error from running python my_app.py

Error merging 'data/config' with schema
Invalid value 'enum1', expected one of [ENUM1, ENUM2]
    full_key: custom_enum
    object_type=CustomConfig

Where my_app.py is just:

cs = ConfigStore.instance()
cs.store(name="base_config", node=Config)
cs.store(group="data", name="config", node=CustomConfig)

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

And the config in data/config.yaml is just

custom_enum: enum1

Solution

  • Note the error message: Invalid value 'enum1', expected one of [ENUM1, ENUM2].

    This is to say, in your data/config.yaml file, you should be using ENUM1 instead of enum1.