From f90dd822d53ae088d42c8611f495cc0818594a0c Mon Sep 17 00:00:00 2001 From: Omry Yadan Date: Tue, 26 Jan 2021 14:03:48 -0800 Subject: [PATCH] Fix interpolation in Hydra config node --- hydra/core/utils.py | 9 ++++----- news/1335.bugfix | 1 + tests/test_hydra.py | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 news/1335.bugfix diff --git a/hydra/core/utils.py b/hydra/core/utils.py index 3fc2ea35e4b..201703acd5c 100644 --- a/hydra/core/utils.py +++ b/hydra/core/utils.py @@ -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 @@ -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) diff --git a/news/1335.bugfix b/news/1335.bugfix new file mode 100644 index 00000000000..08e85f3b4cb --- /dev/null +++ b/news/1335.bugfix @@ -0,0 +1 @@ +Fix interpolation in Hydra config node diff --git a/tests/test_hydra.py b/tests/test_hydra.py index f1d7ef2cbc8..f8c6afa1dd4 100644 --- a/tests/test_hydra.py +++ b/tests/test_hydra.py @@ -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"], + )