2026-06-13 20:31:42 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from copy import deepcopy
|
|
|
|
|
from pathlib import Path
|
mypy: Fix any_generics, redundant_casts, unused_ignores
Add mypy directives disallow_any_generics, warn_redundant_casts, and
warn_unused_ignores. Fix all resulting type annotation errors:
- load_yaml.py: Add type arguments to dict, tuple, set annotations; fix
resolving dict/set types for link keys; handle str|int path types.
- misc.py: Add Mapping[str, Any] and Iterable[Any] type arguments; fix
multiline export comment to avoid bad pattern flag.
- multi_key_dict.py: Add type argument to dict[tuple, Any].
- StringTree.py: Move Any import out of TYPE_CHECKING; fix MutableMapping
type args.
- serdes.py: Add type argument to re.Match.
- Config.py: Change Config from Mapping to Mapping[str, str]; remove
unused cast.
- ev/base/Object.py, SimpleObject.py, EventLogger.py, PollerOneShot.py:
Add Mapping[str, Any] type args.
- ev/app/EventRouter.py: Add dict[str, Any] type args in inner functions.
- auth/*: Remove now-unnecessary # type: ignore[return-value] comments.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.82.0
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-24 19:25:57 +02:00
|
|
|
from typing import Any, cast, TypeAlias
|
2026-06-13 20:31:42 +02:00
|
|
|
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
import re # noqa: E402
|
|
|
|
|
|
2026-06-13 20:31:42 +02:00
|
|
|
import yaml # type: ignore[import-untyped]
|
|
|
|
|
|
2026-07-08 15:37:06 +02:00
|
|
|
IncludeSpec: TypeAlias = tuple[bool, Path, tuple[str, ...]]
|
|
|
|
|
Includes: TypeAlias = list[IncludeSpec]
|
2026-06-13 20:31:42 +02:00
|
|
|
|
mypy: Fix any_generics, redundant_casts, unused_ignores
Add mypy directives disallow_any_generics, warn_redundant_casts, and
warn_unused_ignores. Fix all resulting type annotation errors:
- load_yaml.py: Add type arguments to dict, tuple, set annotations; fix
resolving dict/set types for link keys; handle str|int path types.
- misc.py: Add Mapping[str, Any] and Iterable[Any] type arguments; fix
multiline export comment to avoid bad pattern flag.
- multi_key_dict.py: Add type argument to dict[tuple, Any].
- StringTree.py: Move Any import out of TYPE_CHECKING; fix MutableMapping
type args.
- serdes.py: Add type argument to re.Match.
- Config.py: Change Config from Mapping to Mapping[str, str]; remove
unused cast.
- ev/base/Object.py, SimpleObject.py, EventLogger.py, PollerOneShot.py:
Add Mapping[str, Any] type args.
- ev/app/EventRouter.py: Add dict[str, Any] type args in inner functions.
- auth/*: Remove now-unnecessary # type: ignore[return-value] comments.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.82.0
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-24 19:25:57 +02:00
|
|
|
def _deep_merge(base: dict[str, Any], override: dict[str, Any]) -> dict[str, Any]:
|
2026-06-13 20:31:42 +02:00
|
|
|
ret = deepcopy(base)
|
|
|
|
|
for key, val in override.items():
|
|
|
|
|
if (key in ret and isinstance(ret[key], dict) and isinstance(val, dict)):
|
|
|
|
|
ret[key] = _deep_merge(ret[key], val)
|
|
|
|
|
continue
|
|
|
|
|
ret[key] = deepcopy(val)
|
|
|
|
|
return ret
|
|
|
|
|
|
2026-07-08 15:37:06 +02:00
|
|
|
def _merge_at_path(root: dict[str, Any], path: tuple[str, ...],
|
|
|
|
|
value: Any) -> dict[str, Any]:
|
|
|
|
|
"""Deep-merge *value* into *root* at the given *path*.
|
|
|
|
|
|
|
|
|
|
Creates intermediate dicts as needed. Returns the modified root.
|
|
|
|
|
"""
|
|
|
|
|
if not path:
|
|
|
|
|
return _deep_merge(root, value)
|
|
|
|
|
current = root
|
|
|
|
|
for i, key in enumerate(path[:-1]):
|
|
|
|
|
if key not in current or not isinstance(current[key], dict):
|
|
|
|
|
current[key] = {}
|
|
|
|
|
current = current[key]
|
|
|
|
|
last_key = path[-1]
|
|
|
|
|
if (last_key in current and isinstance(current[last_key], dict)
|
|
|
|
|
and isinstance(value, dict)):
|
|
|
|
|
current[last_key] = _deep_merge(current[last_key], value)
|
|
|
|
|
else:
|
|
|
|
|
current[last_key] = deepcopy(value)
|
|
|
|
|
return root
|
|
|
|
|
|
|
|
|
|
_INCLUDE_PATH_RE = re.compile(r'^([a-zA-Z0-9_.|-]+)\|')
|
|
|
|
|
|
|
|
|
|
def _parse_include_spec(spec: str, parent: Path) -> IncludeSpec:
|
2026-06-13 20:31:42 +02:00
|
|
|
if not spec:
|
2026-07-08 15:37:06 +02:00
|
|
|
raise ValueError('Empty include spec')
|
2026-06-13 20:31:42 +02:00
|
|
|
optional = spec.startswith('-')
|
|
|
|
|
if optional:
|
|
|
|
|
spec = spec[1:]
|
|
|
|
|
if not spec:
|
2026-07-08 15:37:06 +02:00
|
|
|
raise ValueError('Empty include spec after removing optional marker')
|
|
|
|
|
# Check for target path syntax: "path.to.target|/file.yaml" (like named links)
|
|
|
|
|
target_path: tuple[str, ...] = ()
|
|
|
|
|
if '|' in spec:
|
|
|
|
|
m = _INCLUDE_PATH_RE.match(spec)
|
|
|
|
|
if m:
|
|
|
|
|
target_path = tuple(m.group(1).split('.'))
|
|
|
|
|
file_spec = spec[len(m.group(1)) + 1:]
|
|
|
|
|
else:
|
|
|
|
|
file_spec = spec
|
|
|
|
|
else:
|
|
|
|
|
file_spec = spec
|
|
|
|
|
path = Path(file_spec)
|
2026-06-13 20:31:42 +02:00
|
|
|
if not path.is_absolute():
|
|
|
|
|
path = parent / path
|
2026-07-08 15:37:06 +02:00
|
|
|
return (optional, path, target_path)
|
2026-06-13 20:31:42 +02:00
|
|
|
|
|
|
|
|
def _normalize_include_specs(specs: Any, parent: Path) -> Includes:
|
|
|
|
|
if specs is None:
|
|
|
|
|
return []
|
|
|
|
|
if isinstance(specs, str):
|
2026-07-08 15:37:06 +02:00
|
|
|
return [_parse_include_spec(specs, parent)]
|
2026-06-13 20:31:42 +02:00
|
|
|
if isinstance(specs, list):
|
|
|
|
|
ret: Includes = []
|
|
|
|
|
for spec in specs:
|
|
|
|
|
if not isinstance(spec, str):
|
|
|
|
|
raise TypeError(f'Unsupported include entry: {spec!r}')
|
2026-07-08 15:37:06 +02:00
|
|
|
ret.append(_parse_include_spec(spec, parent))
|
2026-06-13 20:31:42 +02:00
|
|
|
return ret
|
|
|
|
|
raise TypeError(f'Unsupported include format: {specs!r}')
|
|
|
|
|
|
|
|
|
|
def _load_yaml(path: str | Path, optional: bool, seen: set[Path]) -> dict[str, Any]:
|
|
|
|
|
path = Path(path).resolve()
|
|
|
|
|
if path in seen:
|
|
|
|
|
raise ValueError(f'Cyclic YAML include detected: {path}')
|
|
|
|
|
seen.add(path)
|
|
|
|
|
try:
|
|
|
|
|
try:
|
|
|
|
|
with open(path) as f:
|
|
|
|
|
loaded = yaml.safe_load(f)
|
|
|
|
|
if loaded is None:
|
|
|
|
|
ret: dict[str, Any] = {}
|
|
|
|
|
elif isinstance(loaded, dict):
|
|
|
|
|
ret = loaded
|
|
|
|
|
else:
|
|
|
|
|
raise TypeError(
|
|
|
|
|
f'YAML root must be a mapping in {path!r}, '
|
|
|
|
|
f'got {type(loaded).__name__}'
|
|
|
|
|
)
|
|
|
|
|
ret = dict(ret)
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
if optional:
|
|
|
|
|
return {}
|
|
|
|
|
raise
|
2026-07-03 17:10:02 +02:00
|
|
|
includes = _normalize_include_specs(ret.get('jw.include', []), path.parent)
|
|
|
|
|
ret.pop('jw.include', None)
|
2026-06-13 20:31:42 +02:00
|
|
|
included_all: dict[str, Any] = {}
|
|
|
|
|
for include in includes:
|
2026-07-08 15:37:06 +02:00
|
|
|
opt, inc_path, target = include
|
|
|
|
|
included_next = _load_yaml(inc_path, optional = opt, seen = seen)
|
|
|
|
|
if target:
|
|
|
|
|
# Merge at specific target path in the main document
|
|
|
|
|
ret = _merge_at_path(ret, target, included_next)
|
|
|
|
|
else:
|
|
|
|
|
included_all = _deep_merge(included_all, included_next)
|
2026-06-13 20:31:42 +02:00
|
|
|
return _deep_merge(included_all, ret)
|
|
|
|
|
finally:
|
|
|
|
|
seen.remove(path)
|
|
|
|
|
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
_LINK_NAME_RE = re.compile(r'^([a-zA-Z0-9_-]+)\|')
|
|
|
|
|
|
|
|
|
|
def _parse_link(value: str) -> tuple[bool, str | None, str]:
|
|
|
|
|
"""Parse a link value into (is_named, name_or_None, path_string).
|
|
|
|
|
|
|
|
|
|
A named link uses a single pipe to separate the link name from the
|
|
|
|
|
target path, e.g. ``"name|path|to|target"``. Otherwise the entire
|
|
|
|
|
string is treated as an absolute or relative target path.
|
|
|
|
|
"""
|
|
|
|
|
m = _LINK_NAME_RE.match(value)
|
|
|
|
|
if m:
|
|
|
|
|
return (True, m.group(1), value[len(m.group(1)) + 1:])
|
|
|
|
|
return (False, None, value)
|
|
|
|
|
|
mypy: Fix any_generics, redundant_casts, unused_ignores
Add mypy directives disallow_any_generics, warn_redundant_casts, and
warn_unused_ignores. Fix all resulting type annotation errors:
- load_yaml.py: Add type arguments to dict, tuple, set annotations; fix
resolving dict/set types for link keys; handle str|int path types.
- misc.py: Add Mapping[str, Any] and Iterable[Any] type arguments; fix
multiline export comment to avoid bad pattern flag.
- multi_key_dict.py: Add type argument to dict[tuple, Any].
- StringTree.py: Move Any import out of TYPE_CHECKING; fix MutableMapping
type args.
- serdes.py: Add type argument to re.Match.
- Config.py: Change Config from Mapping to Mapping[str, str]; remove
unused cast.
- ev/base/Object.py, SimpleObject.py, EventLogger.py, PollerOneShot.py:
Add Mapping[str, Any] type args.
- ev/app/EventRouter.py: Add dict[str, Any] type args in inner functions.
- auth/*: Remove now-unnecessary # type: ignore[return-value] comments.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.82.0
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-24 19:25:57 +02:00
|
|
|
def _resolve_path(
|
|
|
|
|
path_str: str,
|
|
|
|
|
current_path: tuple[str, ...],
|
|
|
|
|
root: dict[str, Any],
|
|
|
|
|
) -> tuple[tuple[str, ...], Any]:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
"""Resolve *path_str* and return ``(path, value)`` at that location.
|
|
|
|
|
|
|
|
|
|
Absolute paths are resolved from the document root; relative paths
|
|
|
|
|
(starting with ``.``) are resolved from *current_path*. Path segments
|
|
|
|
|
are separated by dots.
|
|
|
|
|
"""
|
|
|
|
|
if not path_str:
|
mypy: Fix any_generics, redundant_casts, unused_ignores
Add mypy directives disallow_any_generics, warn_redundant_casts, and
warn_unused_ignores. Fix all resulting type annotation errors:
- load_yaml.py: Add type arguments to dict, tuple, set annotations; fix
resolving dict/set types for link keys; handle str|int path types.
- misc.py: Add Mapping[str, Any] and Iterable[Any] type arguments; fix
multiline export comment to avoid bad pattern flag.
- multi_key_dict.py: Add type argument to dict[tuple, Any].
- StringTree.py: Move Any import out of TYPE_CHECKING; fix MutableMapping
type args.
- serdes.py: Add type argument to re.Match.
- Config.py: Change Config from Mapping to Mapping[str, str]; remove
unused cast.
- ev/base/Object.py, SimpleObject.py, EventLogger.py, PollerOneShot.py:
Add Mapping[str, Any] type args.
- ev/app/EventRouter.py: Add dict[str, Any] type args in inner functions.
- auth/*: Remove now-unnecessary # type: ignore[return-value] comments.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.82.0
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-24 19:25:57 +02:00
|
|
|
resolved_path: tuple[str, ...] = current_path
|
|
|
|
|
target: Any = root
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
for key in current_path:
|
|
|
|
|
target = target[key]
|
|
|
|
|
return (resolved_path, target)
|
|
|
|
|
|
|
|
|
|
if path_str.startswith('.'):
|
|
|
|
|
# Count consecutive leading dots to determine levels to go up
|
|
|
|
|
# from the link's parent. 1 dot = stay, 2 dots = up 1, etc.
|
|
|
|
|
dot_count = 0
|
|
|
|
|
for ch in path_str:
|
|
|
|
|
if ch == '.':
|
|
|
|
|
dot_count += 1
|
|
|
|
|
else:
|
|
|
|
|
break
|
|
|
|
|
levels_up = dot_count - 1
|
|
|
|
|
rest = path_str[dot_count:]
|
|
|
|
|
|
|
|
|
|
# Start from the link's parent (the dict that contains the link)
|
|
|
|
|
if not current_path:
|
mypy: Fix any_generics, redundant_casts, unused_ignores
Add mypy directives disallow_any_generics, warn_redundant_casts, and
warn_unused_ignores. Fix all resulting type annotation errors:
- load_yaml.py: Add type arguments to dict, tuple, set annotations; fix
resolving dict/set types for link keys; handle str|int path types.
- misc.py: Add Mapping[str, Any] and Iterable[Any] type arguments; fix
multiline export comment to avoid bad pattern flag.
- multi_key_dict.py: Add type argument to dict[tuple, Any].
- StringTree.py: Move Any import out of TYPE_CHECKING; fix MutableMapping
type args.
- serdes.py: Add type argument to re.Match.
- Config.py: Change Config from Mapping to Mapping[str, str]; remove
unused cast.
- ev/base/Object.py, SimpleObject.py, EventLogger.py, PollerOneShot.py:
Add Mapping[str, Any] type args.
- ev/app/EventRouter.py: Add dict[str, Any] type args in inner functions.
- auth/*: Remove now-unnecessary # type: ignore[return-value] comments.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.82.0
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-24 19:25:57 +02:00
|
|
|
parent_path: tuple[str, ...] = ()
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
else:
|
|
|
|
|
parent_path = current_path[:-1]
|
|
|
|
|
|
|
|
|
|
if levels_up > 0:
|
|
|
|
|
if levels_up > len(parent_path):
|
|
|
|
|
raise ValueError(f"Link target path goes above root: {path_str!r}")
|
|
|
|
|
new_path = parent_path[:-levels_up]
|
|
|
|
|
else:
|
|
|
|
|
new_path = parent_path
|
|
|
|
|
|
|
|
|
|
# Navigate from root to the base (new_path)
|
|
|
|
|
resolved_path = new_path
|
|
|
|
|
target = root
|
|
|
|
|
for key in new_path:
|
|
|
|
|
target = target[key]
|
|
|
|
|
|
|
|
|
|
if rest:
|
|
|
|
|
for key in rest.split('.'):
|
|
|
|
|
resolved_path = resolved_path + (key, )
|
|
|
|
|
target = target[key]
|
|
|
|
|
|
|
|
|
|
return (resolved_path, target)
|
|
|
|
|
# Absolute path from root
|
|
|
|
|
resolved_path = tuple(path_str.split('.'))
|
|
|
|
|
target = root
|
|
|
|
|
for key in path_str.split('.'):
|
|
|
|
|
target = target[key]
|
|
|
|
|
return (resolved_path, target)
|
|
|
|
|
|
|
|
|
|
def _expand_links_recursive(
|
|
|
|
|
node: Any,
|
|
|
|
|
root: dict[str, Any],
|
mypy: Fix any_generics, redundant_casts, unused_ignores
Add mypy directives disallow_any_generics, warn_redundant_casts, and
warn_unused_ignores. Fix all resulting type annotation errors:
- load_yaml.py: Add type arguments to dict, tuple, set annotations; fix
resolving dict/set types for link keys; handle str|int path types.
- misc.py: Add Mapping[str, Any] and Iterable[Any] type arguments; fix
multiline export comment to avoid bad pattern flag.
- multi_key_dict.py: Add type argument to dict[tuple, Any].
- StringTree.py: Move Any import out of TYPE_CHECKING; fix MutableMapping
type args.
- serdes.py: Add type argument to re.Match.
- Config.py: Change Config from Mapping to Mapping[str, str]; remove
unused cast.
- ev/base/Object.py, SimpleObject.py, EventLogger.py, PollerOneShot.py:
Add Mapping[str, Any] type args.
- ev/app/EventRouter.py: Add dict[str, Any] type args in inner functions.
- auth/*: Remove now-unnecessary # type: ignore[return-value] comments.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.82.0
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-24 19:25:57 +02:00
|
|
|
current_path: tuple[str | int, ...],
|
|
|
|
|
resolved: dict[tuple[tuple[str | int, ...], str], Any],
|
|
|
|
|
resolving: set[tuple[tuple[str | int, ...], str]],
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
) -> Any:
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
"""Recursively walk *node* and expand all link markers.
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
|
|
|
|
|
``resolved`` is a memoisation cache mapping ``(path, link_spec)`` →
|
|
|
|
|
expanded value. ``resolving`` tracks links currently being expanded
|
|
|
|
|
to detect cycles.
|
|
|
|
|
"""
|
|
|
|
|
if isinstance(node, dict):
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
# --- jw.inplace-link: can have siblings, merges into resolved target ---
|
|
|
|
|
if 'jw.inplace-link' in node:
|
|
|
|
|
link_spec = node['jw.inplace-link']
|
|
|
|
|
link_key = (current_path, link_spec)
|
|
|
|
|
|
|
|
|
|
if link_key in resolving:
|
|
|
|
|
raise ValueError(f"Cyclic link detected: {link_spec!r}")
|
|
|
|
|
if link_key in resolved:
|
mypy: Fix any_generics, redundant_casts, unused_ignores
Add mypy directives disallow_any_generics, warn_redundant_casts, and
warn_unused_ignores. Fix all resulting type annotation errors:
- load_yaml.py: Add type arguments to dict, tuple, set annotations; fix
resolving dict/set types for link keys; handle str|int path types.
- misc.py: Add Mapping[str, Any] and Iterable[Any] type arguments; fix
multiline export comment to avoid bad pattern flag.
- multi_key_dict.py: Add type argument to dict[tuple, Any].
- StringTree.py: Move Any import out of TYPE_CHECKING; fix MutableMapping
type args.
- serdes.py: Add type argument to re.Match.
- Config.py: Change Config from Mapping to Mapping[str, str]; remove
unused cast.
- ev/base/Object.py, SimpleObject.py, EventLogger.py, PollerOneShot.py:
Add Mapping[str, Any] type args.
- ev/app/EventRouter.py: Add dict[str, Any] type args in inner functions.
- auth/*: Remove now-unnecessary # type: ignore[return-value] comments.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.82.0
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-24 19:25:57 +02:00
|
|
|
base_value: dict[str, Any] | Any = resolved[link_key]
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
else:
|
|
|
|
|
resolving.add(link_key)
|
|
|
|
|
_, _, path = _parse_link(link_spec)
|
mypy: Fix any_generics, redundant_casts, unused_ignores
Add mypy directives disallow_any_generics, warn_redundant_casts, and
warn_unused_ignores. Fix all resulting type annotation errors:
- load_yaml.py: Add type arguments to dict, tuple, set annotations; fix
resolving dict/set types for link keys; handle str|int path types.
- misc.py: Add Mapping[str, Any] and Iterable[Any] type arguments; fix
multiline export comment to avoid bad pattern flag.
- multi_key_dict.py: Add type argument to dict[tuple, Any].
- StringTree.py: Move Any import out of TYPE_CHECKING; fix MutableMapping
type args.
- serdes.py: Add type argument to re.Match.
- Config.py: Change Config from Mapping to Mapping[str, str]; remove
unused cast.
- ev/base/Object.py, SimpleObject.py, EventLogger.py, PollerOneShot.py:
Add Mapping[str, Any] type args.
- ev/app/EventRouter.py: Add dict[str, Any] type args in inner functions.
- auth/*: Remove now-unnecessary # type: ignore[return-value] comments.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.82.0
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-24 19:25:57 +02:00
|
|
|
# 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
|
|
|
|
|
)
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
base_value = _expand_links_recursive(
|
2026-07-08 15:37:06 +02:00
|
|
|
target_value, root, target_path, resolved, resolving
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
)
|
|
|
|
|
resolved[link_key] = base_value
|
|
|
|
|
resolving.discard(link_key)
|
|
|
|
|
|
|
|
|
|
# Expand sibling keys
|
|
|
|
|
expanded_remaining: dict[str, Any] = {}
|
|
|
|
|
for key, value in node.items():
|
|
|
|
|
if key == 'jw.inplace-link':
|
|
|
|
|
continue
|
|
|
|
|
expanded_remaining[key] = _expand_links_recursive(
|
|
|
|
|
value, root, current_path + (key, ), resolved, resolving
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Deep-merge siblings into resolved dict (siblings win on conflict)
|
|
|
|
|
if isinstance(base_value, dict) and expanded_remaining:
|
|
|
|
|
return _deep_merge(base_value, expanded_remaining)
|
|
|
|
|
return base_value
|
|
|
|
|
|
|
|
|
|
# --- jw.link (sole key): inplace or named replacement ---
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
if 'jw.link' in node and len(node) == 1:
|
|
|
|
|
link_spec = node['jw.link']
|
|
|
|
|
link_key = (current_path, link_spec)
|
|
|
|
|
|
|
|
|
|
if link_key in resolving:
|
|
|
|
|
raise ValueError(f"Cyclic link detected: {link_spec!r}")
|
|
|
|
|
if link_key in resolved:
|
|
|
|
|
return resolved[link_key]
|
|
|
|
|
|
|
|
|
|
resolving.add(link_key)
|
|
|
|
|
is_named, name, path = _parse_link(link_spec)
|
mypy: Fix any_generics, redundant_casts, unused_ignores
Add mypy directives disallow_any_generics, warn_redundant_casts, and
warn_unused_ignores. Fix all resulting type annotation errors:
- load_yaml.py: Add type arguments to dict, tuple, set annotations; fix
resolving dict/set types for link keys; handle str|int path types.
- misc.py: Add Mapping[str, Any] and Iterable[Any] type arguments; fix
multiline export comment to avoid bad pattern flag.
- multi_key_dict.py: Add type argument to dict[tuple, Any].
- StringTree.py: Move Any import out of TYPE_CHECKING; fix MutableMapping
type args.
- serdes.py: Add type argument to re.Match.
- Config.py: Change Config from Mapping to Mapping[str, str]; remove
unused cast.
- ev/base/Object.py, SimpleObject.py, EventLogger.py, PollerOneShot.py:
Add Mapping[str, Any] type args.
- ev/app/EventRouter.py: Add dict[str, Any] type args in inner functions.
- auth/*: Remove now-unnecessary # type: ignore[return-value] comments.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.82.0
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-24 19:25:57 +02:00
|
|
|
# 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
|
|
|
|
|
)
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
expanded_target = _expand_links_recursive(
|
|
|
|
|
target_value, root, target_path, resolved, resolving
|
|
|
|
|
)
|
|
|
|
|
resolving.discard(link_key)
|
|
|
|
|
|
|
|
|
|
if is_named:
|
|
|
|
|
result = {name: expanded_target}
|
|
|
|
|
else:
|
|
|
|
|
result = expanded_target
|
|
|
|
|
|
|
|
|
|
resolved[link_key] = result
|
|
|
|
|
return result
|
|
|
|
|
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
# --- Normal dict — expand values recursively ---
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
expanded: dict[str, Any] = {}
|
|
|
|
|
for key, value in node.items():
|
|
|
|
|
expanded[key] = _expand_links_recursive(
|
|
|
|
|
value, root, current_path + (key, ), resolved, resolving
|
|
|
|
|
)
|
|
|
|
|
return expanded
|
|
|
|
|
|
|
|
|
|
if isinstance(node, list):
|
|
|
|
|
return [
|
|
|
|
|
_expand_links_recursive(
|
|
|
|
|
item, root, current_path + (i, ), resolved, resolving
|
|
|
|
|
) for i, item in enumerate(node)
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
return node # scalar or None
|
|
|
|
|
|
|
|
|
|
def _expand_links(root: dict[str, Any]) -> dict[str, Any]:
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
r"""Expand all link markers in *root* and return the result.
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
Three link forms are supported:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
- **Inplace** ``jw.link: "target.path"`` — the ``jw.link`` key
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
disappears and is replaced by the resolved value, inline.
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
- **Named** ``jw.link: "name|target.path"`` — the ``jw.link`` key
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
is renamed to ``name`` and its value is the resolved target.
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
- **Inplace** ``jw.inplace-link: "target.path"`` — the ``jw.inplace-link``
|
|
|
|
|
key disappears and its value is deep-merged into the resolved target
|
|
|
|
|
dict. Sibling keys (if any) override the target on conflict.
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
|
|
|
|
|
Paths use dot-separated segments. A relative path starts with ``.``
|
|
|
|
|
(Python-import style): ``.`` stays at the current level, ``..`` goes
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
up one level, ``...`` two, and so on. The first ``|`` in the link
|
|
|
|
|
value (only when the prefix matches ``[a-zA-Z0-9_-]+|``) separates
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
the link name from the target path for named links.
|
|
|
|
|
|
|
|
|
|
Cyclic links raise ``ValueError``; missing targets also raise.
|
|
|
|
|
"""
|
mypy: Fix any_generics, redundant_casts, unused_ignores
Add mypy directives disallow_any_generics, warn_redundant_casts, and
warn_unused_ignores. Fix all resulting type annotation errors:
- load_yaml.py: Add type arguments to dict, tuple, set annotations; fix
resolving dict/set types for link keys; handle str|int path types.
- misc.py: Add Mapping[str, Any] and Iterable[Any] type arguments; fix
multiline export comment to avoid bad pattern flag.
- multi_key_dict.py: Add type argument to dict[tuple, Any].
- StringTree.py: Move Any import out of TYPE_CHECKING; fix MutableMapping
type args.
- serdes.py: Add type argument to re.Match.
- Config.py: Change Config from Mapping to Mapping[str, str]; remove
unused cast.
- ev/base/Object.py, SimpleObject.py, EventLogger.py, PollerOneShot.py:
Add Mapping[str, Any] type args.
- ev/app/EventRouter.py: Add dict[str, Any] type args in inner functions.
- auth/*: Remove now-unnecessary # type: ignore[return-value] comments.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.82.0
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-24 19:25:57 +02:00
|
|
|
resolved: dict[tuple[tuple[str | int, ...], str], Any] = {}
|
|
|
|
|
resolving: set[tuple[tuple[str | int, ...], str]] = set()
|
2026-07-24 21:44:20 +02:00
|
|
|
return cast(
|
|
|
|
|
'dict[str, Any]', _expand_links_recursive(root, root, (), resolved, resolving)
|
|
|
|
|
)
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
|
2026-06-13 20:31:42 +02:00
|
|
|
def load_yaml(path: str | Path, optional: bool = False) -> dict[str, Any]: # export
|
|
|
|
|
seen: set[Path] = set()
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
return _expand_links(_load_yaml(path, optional, seen))
|
2026-06-13 20:31:42 +02:00
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
import json
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
print(json.dumps(load_yaml(sys.argv[1]), indent = 2))
|