facebookresearch / hydra

Hydra is a framework for elegantly configuring complex applications
https://hydra.cc
MIT License
8.82k stars 635 forks source link

[Bug] AssertionError when configuring hydra from top-level dataclass #2348

Open Jasha10 opened 2 years ago

Jasha10 commented 2 years ago

🐛 Bug: AssertionError when configuring hydra from top-level dataclass

Description

Using a dataclass at the top-level would be convenient (as it eliminates the need for yaml files), but the below usage pattern results in an AssertionError.

To reproduce

# repro.py
from dataclasses import dataclass, field
from typing import Any, Dict
import hydra
from hydra.core.config_store import ConfigStore

@dataclass
class MyAppConfig:
    req_int: int
    opt_str: str = "Default String"
    opt_float: float = 3.14

    hydra: Dict[str, Any] = field(default_factory=lambda: {"job": {"chdir": False}})

cs = ConfigStore.instance()
# Registering the Config class with the name 'base_config'.
cs.store(name="base_config", node=MyAppConfig)

@hydra.main(version_base=None, config_name="base_config")
def my_app(cfg: MyAppConfig) -> None:
    print(cfg)

if __name__ == "__main__":
    my_app()
$ python repro.py
Traceback (most recent call last):
  File "/home/rig1/dev/hydra/hydra/_internal/utils.py", line 217, in run_and_report
    return func()
  File "/home/rig1/dev/hydra/hydra/_internal/utils.py", line 457, in <lambda>
    lambda: hydra.run(
  File "/home/rig1/dev/hydra/hydra/_internal/hydra.py", line 115, in run
    assert cfg.hydra.mode == RunMode.RUN
AssertionError

System information

Additional context

The assertion error in the traceback happens because the value of cfg.hydra.mode is the string "RUN", which is not equal to the enum member RunMode.RUN. There might be an underlying OmegaConf issue here.

Thanks to Omry for reporting this one.

Jasha10 commented 2 years ago

I've opened omegaconf issue https://github.com/omry/omegaconf/issues/998 which causes this issue.