From 1fd3c75c0862e60ad7469a81b4cc385c30dc2e08 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Sun, 16 Aug 2026 12:18:55 +0200 Subject: [PATCH] load_yaml: Reject non-string link values A link value that is not a string (for example a number parsed from YAML) is passed to the regular expression in _parse_link(), which crashes with a bare TypeError from the regex engine that does not mention the offending value or the link kind. Make _resolve_link() validate the spec type up front and raise TypeError naming the link kind and the value. Add tests for both jw.link and jw.inplace-link. Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2 Signed-off-by: Jan Lindemann --- src/python/jw/util/load_yaml.py | 13 +++++++++---- test/unit/python/jw/util/load_yaml/test.py | 10 ++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/python/jw/util/load_yaml.py b/src/python/jw/util/load_yaml.py index b195b3e..45c2ce7 100644 --- a/src/python/jw/util/load_yaml.py +++ b/src/python/jw/util/load_yaml.py @@ -187,11 +187,16 @@ def _resolve_link( ) -> 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. + Returns ``(is_named, name, target_path, target_value)``. A + non-string spec raises ``TypeError``; 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. """ + if not isinstance(spec, str): + raise TypeError( + f'{kind} value must be a string, got {type(spec).__name__}: {spec!r}' + ) is_named, name, path = _parse_link(spec) where = '.'.join(str(part) for part in current_path) if not where: diff --git a/test/unit/python/jw/util/load_yaml/test.py b/test/unit/python/jw/util/load_yaml/test.py index e051070..6222802 100644 --- a/test/unit/python/jw/util/load_yaml/test.py +++ b/test/unit/python/jw/util/load_yaml/test.py @@ -636,6 +636,16 @@ class TestEdgeCases: data = load_yaml(yaml_file('target: ok\nlink:\n jw.link: 123target|target\n')) assert data['link'] == {'123target': 'ok'} + def test_non_string_link_value_raises(self, yaml_file: Callable) -> None: + """Non-string jw.link values raise TypeError naming the value.""" + with pytest.raises(TypeError, match = 'must be a string'): + load_yaml(yaml_file('target: 42\nlink:\n jw.link: 42\n')) + + def test_non_string_inplace_link_value_raises(self, yaml_file: Callable) -> None: + """Non-string jw.inplace-link values raise TypeError.""" + with pytest.raises(TypeError, match = 'must be a string'): + load_yaml(yaml_file('target:\n a: 1\nentry:\n jw.inplace-link: 42\n')) + # -- jw.inplace-link expansion ------------------------------------------------ class TestInplaceLink: