Skip to content

Commit

Permalink
Fix error when full_key is an int
Browse files Browse the repository at this point in the history
  • Loading branch information
jesszzzz committed Feb 3, 2025
1 parent 7c8fa4a commit 2a53d55
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions hydra/_internal/instantiate/_instantiate2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 16 additions & 0 deletions tests/instantiate/test_instantiate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 2a53d55

Please sign in to comment.