diff --git a/hydra/_internal/instantiate/_instantiate2.py b/hydra/_internal/instantiate/_instantiate2.py index bc27918274..aee06438d1 100644 --- a/hydra/_internal/instantiate/_instantiate2.py +++ b/hydra/_internal/instantiate/_instantiate2.py @@ -151,14 +151,14 @@ def _deep_copy_full_config(subconfig: Any) -> Any: return copy.deepcopy(subconfig) full_key = subconfig._get_full_key(None) - if not full_key: + if full_key == '' or full_key is None: return copy.deepcopy(subconfig) if OmegaConf.is_list(subconfig._get_parent()): # OmegaConf has a bug where _get_full_key doesn't add [] if the parent # is a list, eg. instead of foo[0], it'll return foo0 index = subconfig._key() - full_key = full_key[: -len(str(index))] + f"[{index}]" + full_key = str(full_key)[: -len(str(index))] + f"[{index}]" root = subconfig._get_root() full_key = full_key.replace(root._get_full_key(None) or "", "", 1) if OmegaConf.select(root, full_key) is not subconfig: diff --git a/tests/instantiate/test_instantiate.py b/tests/instantiate/test_instantiate.py index 6a89979074..8c64678343 100644 --- a/tests/instantiate/test_instantiate.py +++ b/tests/instantiate/test_instantiate.py @@ -650,6 +650,22 @@ def test_class_instantiate_omegaconf_node(instantiate_func: Any, config: Any) -> assert obj == AClass(a=10, b=200, c={"x": 10, "y": 200}, d=AnotherClass(99)) assert OmegaConf.is_config(obj.c) +@mark.parametrize( + "src", + [ + ( + ListConfig([{ + "_target_": "tests.instantiate.AClass", + "b": 200, + "c": {"x": 10, "y": "${b}"}, + }]) + ) + ], +) +def test_class_instantiate_list_item(instantiate_func: Any, config: Any) -> Any: + obj = instantiate_func(config[0], a=10, d=AnotherClass(99))[0] + assert obj == AClass(a=10, b=200, c={"x": 10, "y": 200}, d=AnotherClass(99)) + assert OmegaConf.is_config(obj.c) @mark.parametrize("src", [{"_target_": "tests.instantiate.Adam"}]) def test_instantiate_adam(instantiate_func: Any, config: Any) -> None: