Skip to content

Commit

Permalink
Fix interpolation in Hydra config node
Browse files Browse the repository at this point in the history
  • Loading branch information
omry committed Jan 26, 2021
1 parent 98c2795 commit f90dd82
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
9 changes: 4 additions & 5 deletions hydra/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ def run_job(
configure_logging: bool = True,
) -> "JobReturn":
old_cwd = os.getcwd()
working_dir = str(OmegaConf.select(config, job_dir_key))
orig_hydra_cfg = HydraConfig.instance().cfg
HydraConfig.instance().set_config(config)
working_dir = str(OmegaConf.select(config, job_dir_key))
if job_subdir_key is not None:
# evaluate job_subdir_key lazily.
# this is running on the client side in sweep and contains things such as job:id which
Expand All @@ -98,15 +99,13 @@ def run_job(
ret = JobReturn()
ret.working_dir = working_dir
task_cfg = copy.deepcopy(config)
hydra_cfg = OmegaConf.masked_copy(task_cfg, "hydra")
# maintain parent to preserve interpolation links from hydra_cfg to job_cfg
hydra_cfg._set_parent(task_cfg)
with read_write(task_cfg):
with open_dict(task_cfg):
del task_cfg["hydra"]
HydraConfig.instance().cfg = hydra_cfg # type: ignore

ret.cfg = task_cfg
hydra_cfg = copy.deepcopy(HydraConfig.instance().cfg)
assert isinstance(hydra_cfg, DictConfig)
ret.hydra_cfg = hydra_cfg
overrides = OmegaConf.to_container(config.hydra.overrides.task)
assert isinstance(overrides, list)
Expand Down
1 change: 1 addition & 0 deletions news/1335.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix interpolation in Hydra config node
22 changes: 22 additions & 0 deletions tests/test_hydra.py
Original file line number Diff line number Diff line change
Expand Up @@ -1166,3 +1166,25 @@ def test_structured_with_none_list(monkeypatch: Any, tmpdir: Path) -> None:
]
result, _err = get_run_output(cmd)
assert result == "{'list': None}"


def test_self_hydra_config_interpolation_integration(tmpdir: Path) -> None:
cfg = OmegaConf.create()
integration_test(
tmpdir=tmpdir,
task_config=cfg,
overrides=[],
prints="HydraConfig.get().job_logging.handlers.file.filename",
expected_outputs="task.log",
)


def test_job_id_and_num_in_sweep(tmpdir: Path) -> None:
cfg = OmegaConf.create()
integration_test(
tmpdir=tmpdir,
task_config=cfg,
overrides=["-m"],
prints=["HydraConfig.get().job.id", "str(HydraConfig.get().job.num)"],
expected_outputs=["0", "0"],
)

0 comments on commit f90dd82

Please sign in to comment.