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 2 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
5 changes: 4 additions & 1 deletion hydra/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ 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)
if config._get_parent() is not None:
shagunsodhani marked this conversation as resolved.
Show resolved Hide resolved
config_copy._set_parent(config._get_parent())
config = config_copy

try:
clazz = get_class(config["class"])
Expand Down
29 changes: 29 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,35 @@ def test_class_instantiate_passthrough(
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},
}
},
Bar(10, 20, 30, 40),
),
],
)
def test_string_iterpolation_during_deepcopy(
input_conf: Dict[str, Any], expected: Any
) -> None:
# Check if the instantiate method maintains the parent when making a deepcopy
conf = OmegaConf.create(input_conf)
orig = copy.deepcopy(conf)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to do the deepcopy here.
Instantiate is doing the deepcopy.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are also testing passthrough.
just make your test another case of test_class_instantiate.
(You can add additional parameters set to the list there).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can get rid of the deepcopy part. Regarding adding extra params to test_class_instantiate, I think it will make it a little messy as we need a nested config and need to call instantiate over a specific node in the config. If that is fine, I can merge the two testcases.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as the test code is the same (and it can be), you can reduce duplication with parametrize.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merged the test cases

assert isinstance(conf, DictConfig)
obj = utils.instantiate(conf.all_params.main, d=40)
assert orig == conf
assert obj == expected


def test_get_original_cwd() -> None:
orig = "/foo/bar"
cfg = OmegaConf.create({"hydra": {"runtime": {"cwd": orig}}})
Expand Down