load_yaml: _resolve_link(): Add context to errors

When a link's target path cannot be resolved, the raw KeyError from
_resolve_path() propagates to the caller (or a raw TypeError when a
path segment hits a list), without saying which link or which
document location caused the failure.  The parse-and-resolve
sequence is also duplicated in the jw.link and jw.inplace-link
branches of _expand_links_recursive().

Extract the sequence into _resolve_link() and wrap navigation
failures in ValueError naming the link kind, the spec, and the
location in the document.  Document that path navigation operates on
the unexpanded tree, so intermediate segments must not be link nodes
themselves.  Update the tests that pinned KeyError and add coverage
for the list-navigation case and the error message content.

Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-08-16 12:18:22 +02:00
commit 503c620341
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61

View file

@ -179,6 +179,35 @@ def _merge_at_path(root: dict[str, Any], path: tuple[str, ...],
# -- Link expansion -------------------------------------------------------------
def _resolve_link(
kind: str,
spec: Any,
current_path: tuple[str | int, ...],
root: dict[str, Any],
) -> tuple[bool, str | None, tuple[str, ...], Any]:
"""Parse *spec* and resolve its target path against *root*.
Returns ``(is_named, name, target_path, target_value)``. A missing
or invalid target raises ``ValueError`` naming the link and its
location. Path navigation operates on the unexpanded tree, so
intermediate segments must not be link nodes themselves.
"""
is_named, name, path = _parse_link(spec)
where = '.'.join(str(part) for part in current_path)
if not where:
where = '<root>'
try:
# current_path may contain int indices from list traversal,
# but _resolve_path only uses string keys for navigation
target_path, target_value = _resolve_path(
path, cast('tuple[str, ...]', current_path), root
)
except (KeyError, IndexError, TypeError) as exc:
raise ValueError(
f'{kind} {spec!r} at {where}: cannot resolve target path: {exc}'
) from None
return (is_named, name, target_path, target_value)
def _expand_links_recursive(
node: Any,
root: dict[str, Any],
@ -204,11 +233,8 @@ def _expand_links_recursive(
base_value: dict[str, Any] | Any = resolved[link_key]
else:
resolving.add(link_key)
_, _, path = _parse_link(link_spec)
# current_path may contain int indices from list traversal,
# but _resolve_path only uses string keys for navigation
target_path, target_value = _resolve_path(
path, cast('tuple[str, ...]', current_path), root
_, _, target_path, target_value = _resolve_link(
'jw.inplace-link', link_spec, current_path, root
)
base_value = _expand_links_recursive(
target_value, root, target_path, resolved, resolving
@ -246,11 +272,8 @@ def _expand_links_recursive(
return resolved[link_key]
resolving.add(link_key)
is_named, name, path = _parse_link(link_spec)
# current_path may contain int indices from list traversal,
# but _resolve_path only uses string keys for navigation
target_path, target_value = _resolve_path(
path, cast('tuple[str, ...]', current_path), root
is_named, name, target_path, target_value = _resolve_link(
'jw.link', link_spec, current_path, root
)
expanded_target = _expand_links_recursive(
target_value, root, target_path, resolved, resolving