test: Fix mypy strict errors in test tree

The test tree needs to be aligned with type checking, too, because jw-pkg
is going to required it for test, too. The load_yaml and StringTree tests
receive their fixtures as bare Callable and Generator without type
arguments, and the StringTree tests pass the st fixture without an
annotation. The cast script suppresses errors with un-coded type ignore
comments, and the parse_opts script declares its loop variable as
"str | int", so indexing the Options object with it is rejected.

Introduce _YamlFile, _TempDir, and _St aliases for the fixture types and
give the st parameters an annotation. Code the type ignore comments with
the argument mismatch they suppress, and split the parse_opts loop variable
into i for the index loops and k for the key loops.

Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.85.1
Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-09-11 17:00:03 +02:00
commit 7885f69b44
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61

View file

@ -6,8 +6,10 @@ import pytest
from jw.util.stree.StringTree import StringTree
_St = Callable[[str], StringTree]
@pytest.fixture
def st() -> Callable[[str], StringTree]:
def st() -> _St:
"""Helper that creates a StringTree from raw config text."""
def _load(text: str) -> StringTree:
@ -48,7 +50,7 @@ def st() -> Callable[[str], StringTree]:
('[section]\nkey = [bracket]\n', '[bracket]'),
]
)
def test_value(st, input: str, expected: str) -> None:
def test_value(st: _St, input: str, expected: str) -> None:
tree = st(input)
assert tree.value('section.key') == expected
@ -61,48 +63,48 @@ def test_value(st, input: str, expected: str) -> None:
('nonexistent.key'), # missing section entirely
]
)
def test_missing_returns_none(st, path: str) -> None:
def test_missing_returns_none(st: _St, path: str) -> None:
"""Both missing key and missing section return None (no default needed)."""
tree = st('[section]\nkey = val\n')
assert tree.value(path) is None
def test_missing_key_with_default_none(st) -> None:
def test_missing_key_with_default_none(st: _St) -> None:
"""value(section.missing, default=None) returns None."""
tree = st('[section]\nother = val\n')
assert tree.value('section.missing', default = None) is None
def test_missing_section_with_default_none(st) -> None:
def test_missing_section_with_default_none(st: _St) -> None:
"""value(nonexistent.key, default=None) returns None."""
tree = st('[section]\nkey = val\n')
assert tree.value('nonexistent.key', default = None) is None
# -- Multiple keys and sections -----------------------------------------------
def test_multiple_keys(st) -> None:
def test_multiple_keys(st: _St) -> None:
tree = st('[section]\nfirst = one\nsecond = two\nthird = three\n')
assert tree.value('section.first') == 'one'
assert tree.value('section.second') == 'two'
assert tree.value('section.third') == 'three'
def test_multiple_sections(st) -> None:
def test_multiple_sections(st: _St) -> None:
tree = st('[one]\nkey = val1\n[two]\nkey = val2\n')
assert tree.value('one.key') == 'val1'
assert tree.value('two.key') == 'val2'
# -- Edge cases ---------------------------------------------------------------
def test_not_an_assignment_returns_none(st) -> None:
def test_not_an_assignment_returns_none(st: _St) -> None:
tree = st('[section]\nnot an assignment\n')
assert tree.value('section.not an assignment') is None
def test_empty_key_raises(st) -> None:
def test_empty_key_raises(st: _St) -> None:
"""Empty key raises StringTree.Error during parsing."""
with pytest.raises(StringTree.Error):
st('[section]\n= value\n')
# -- Missing section (no-op, originally had no assertion) ---------------------
def test_missing_section_noop(st) -> None:
def test_missing_section_noop(st: _St) -> None:
"""Previously this test called value() without asserting anything;
add an explicit check so the test is meaningful."""
tree = st('[section]\nkey = val\n')
@ -111,13 +113,13 @@ def test_missing_section_noop(st) -> None:
# -- Line continuation (Not yet implemented) ----------------------------------
#
# @pytest.mark.skip(reason="Line continuation not yet implemented")
# def test_line_continuation(st):
# def test_line_continuation(st: _St):
# tree = st('[section]\nkey = first \\\n second \\\n third\n')
# assert tree.value('section.key') == 'first second third'
#
#
# @pytest.mark.skip(reason="Line continuation not yet implemented")
# def test_line_continuation_with_comment(st):
# def test_line_continuation_with_comment(st: _St):
# tree = st('[section]\nkey = first \\\n second # comment\n')
# assert tree.value('section.key') == 'first second'
#