Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parent node's information is lost when performing deepcopy. #388

Merged
merged 5 commits into from
Jan 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repos:
rev: 3.7.9
hooks:
- id: flake8
additional_dependencies: [-e, git+git://github.com/pycqa/pyflakes.git@1911c20#egg=pyflakes]
additional_dependencies: [-e, "git+git://github.com/pycqa/pyflakes.git@1911c20#egg=pyflakes"]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.761
Expand Down
4 changes: 3 additions & 1 deletion hydra/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ def instantiate(config: DictConfig, *args: Any, **kwargs: Any) -> Any:

assert config is not None, "Input config is None"
# copy config to avoid mutating it when merging with kwargs
config = copy.deepcopy(config)
config_copy = copy.deepcopy(config)
config_copy._set_parent(config._get_parent())
config = config_copy

try:
clazz = get_class(config["class"])
Expand Down
1 change: 1 addition & 0 deletions news/388.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug with utils.instantiate() failing if params contains interpolated values.
56 changes: 34 additions & 22 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import copy
from pathlib import Path
from typing import Any, Dict
from typing import Any, Dict, Optional

import pytest
from omegaconf import DictConfig, OmegaConf
Expand Down Expand Up @@ -63,45 +62,58 @@ def test_get_static_method(path: str, return_value: Any) -> None:


@pytest.mark.parametrize( # type: ignore
"input_conf, expected",
"input_conf, key_to_get_config, kwargs_to_pass, expected",
[
(
{
"class": "tests.test_utils.Bar",
"params": {"a": 10, "b": 20, "c": 30, "d": 40},
},
None,
{},
Bar(10, 20, 30, 40),
)
],
)
def test_class_instantiate(input_conf: Dict[str, Any], expected: Any) -> Any:
conf = OmegaConf.create(input_conf)
assert isinstance(conf, DictConfig)
obj = utils.instantiate(conf)
assert obj == expected


@pytest.mark.parametrize( # type: ignore
"input_conf, expected",
[
),
(
{
"all_params": {
"main": {
"class": "tests.test_utils.Bar",
"params": {"a": 10, "b": 20, "c": "${all_params.aux.c}"},
},
"aux": {"c": 30},
}
},
"all_params.main",
{"d": 40},
Bar(10, 20, 30, 40),
),
(
{"class": "tests.test_utils.Bar", "params": {"b": 20, "c": 30}},
None,
{"a": 10, "d": 40},
Bar(10, 20, 30, 40),
),
(
{"class": "tests.test_utils.Bar", "params": {"b": 200, "c": "${params.b}"}},
None,
{"a": 10, "d": 40},
Bar(10, 200, 200, 40),
),
],
)
def test_class_instantiate_passthrough(
input_conf: Dict[str, Any], expected: Any
) -> None:
def test_class_instantiate(
input_conf: Dict[str, Any],
key_to_get_config: Optional[str],
kwargs_to_pass: Dict[str, Any],
expected: Any,
) -> Any:
conf = OmegaConf.create(input_conf)
orig = copy.deepcopy(conf)
assert isinstance(conf, DictConfig)
obj = utils.instantiate(conf, 10, d=40)
assert orig == conf
if key_to_get_config is None:
config_to_pass = conf
else:
config_to_pass = conf.select(key_to_get_config)
obj = utils.instantiate(config_to_pass, **kwargs_to_pass)
assert obj == expected


Expand Down