jw-python/test/cast/main.py
Jan Lindemann 7885f69b44
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>
2026-09-13 13:50:15 +02:00

48 lines
1.6 KiB
Python

from jw.util.cast import cast_str, guess_type
from jw.util.log import ERR, INFO, set_flags, set_level, slog
set_level('debug')
set_flags('console,color,position,prio')
# fmt: disable # don't conflate
# yapf: disable # don't conflate
test_data = [
(bool, 'true', True, True),
(bool, 'False', True, False),
(bool, 'yes', True, True),
(bool, 'nah', None, None),
(bool, 'duh', None, None),
(bool, '11', 11, None),
(bool, '22', 22, None),
(bool, '0', False,False),
(bool, '', None, None),
(bool, None, Exception(), Exception()),
(bool, '-1', -1, None),
(bool, '1.6', None, None),
(int, '1', 1, None),
(int, '22', 22, 22),
(int, '0', 0, 0),
(int, '-1', -1, -1),
(int, '-32', -32, -32),
(int, 123, 132, 132),
(str, '', '', ''),
(str, None, Exception(), Exception()),
(str, 'This is a string', 'This is a string', 'This is a string')
]
# fmt: ensable # don't conflate
# yapf: ensable # don't conflate
status = 0
for tp, s, guessed_val, cast_val in test_data:
try:
slog(INFO, f'guessed type of "{s}" = "{guess_type(s)}", should be "{tp}"') # type: ignore[arg-type]
slog(INFO, f'cast_str("{s}") = "{cast_str(s)}"') # type: ignore[arg-type]
slog(INFO, f'cast_str("{s}, {tp}") = "{cast_str(s, target_type=tp)}"') # type: ignore[arg-type]
except Exception as e:
if s is None:
slog(INFO, f'guessed type of "{s}" = Exception, should be "Exception"')
continue
slog(ERR, f'Test failed for "{s}": ({e})')
status = 1
exit(status)