Skip to content

Commit

Permalink
Fix failure when a link target has an undefined parent (#668)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Mauricio Villegas <[email protected]>
  • Loading branch information
DianLiI and mauvilsa authored Feb 4, 2025
1 parent f786412 commit 4b956d4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Fixed
<https://github.com/omni-us/jsonargparse/pull/662>`__).
- Pydantic discriminated unions handled incorrectly (`#667
<https://github.com/omni-us/jsonargparse/pull/667>`__).
- Failure when a link target has an undefined parent (`#668
<https://github.com/omni-us/jsonargparse/pull/668>`__)


v4.36.0 (2025-01-17)
Expand Down
5 changes: 4 additions & 1 deletion jsonargparse/_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -1374,7 +1374,10 @@ def adapt_class_type(

parent_key, key = target.split(split, maxsplit=1)

action = next(a for a in parser._actions if a.dest == parent_key)
try:
action = next(a for a in parser._actions if a.dest == parent_key)
except StopIteration:
continue

sub_add_kwargs = getattr(action, "sub_add_kwargs")
sub_add_kwargs.setdefault("linked_targets", set())
Expand Down
21 changes: 21 additions & 0 deletions jsonargparse_tests/test_link_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,27 @@ def test_on_instantiate_linking_deep_targets_mapping(parser, tmp_path):
assert isinstance(config_init["b"].a_map["name"].d, DeepD)


def test_on_instantiate_linking_deep_targets_undefined_parent(parser, tmp_path):
config = {
"b": {
"class_path": f"{__name__}.DeepBSuper",
},
"c": {},
}
config_path = tmp_path / "config.yaml"
config_path.write_text(json_or_yaml_dump(config))

parser.add_argument("--config", action="config")
parser.add_subclass_arguments(DeepBSuper, nested_key="b", required=True)
parser.add_class_arguments(DeepC, nested_key="c")
# .init_args.a is undefined in DeepBSuper
parser.link_arguments("c", "b.init_args.a.init_args.d", compute_fn=DeepC.fn, apply_on="instantiate")

config = parser.parse_args([f"--config={config_path}"])
config_init = parser.instantiate_classes(config)
assert isinstance(config_init["b"], DeepBSuper)


class DeepTarget:
def __init__(self, a: int, b: int) -> None:
self.a = a
Expand Down

0 comments on commit 4b956d4

Please sign in to comment.