0

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
Jasha
  • 5,507
  • 2
  • 33
  • 44
jlcv
  • 1,688
  • 5
  • 21
  • 50
  • A word of caution: OmegaConf and Hydra do not currently have any test coverage for classes that simultaneously subclass `str` and `Enum`, as is the case with your `CustomEnum` above. – Jasha Dec 16 '21 at 17:39

1 Answers1

1

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.

Jasha
  • 5,507
  • 2
  • 33
  • 44