Coverage for src / rtflite / type_guards.py: 87%
15 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-02 02:44 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-02 02:44 +0000
1"""Type guards for RTF components to handle Union types safely."""
3from collections.abc import Sequence
4from typing import Any, TypeGuard
6from .input import RTFBody, RTFColumnHeader
9def is_single_header(
10 header: RTFColumnHeader | Sequence[RTFColumnHeader | None] | None,
11) -> TypeGuard[RTFColumnHeader]:
12 """Check if header is a single RTFColumnHeader instance."""
13 return header is not None and not isinstance(header, (list, tuple))
16def is_single_body(body: RTFBody | list[RTFBody] | None) -> TypeGuard[RTFBody]:
17 """Check if body is a single RTFBody instance."""
18 return isinstance(body, RTFBody)
21def is_list_header(
22 header: RTFColumnHeader | Sequence[RTFColumnHeader | None] | None,
23) -> TypeGuard[Sequence[RTFColumnHeader | None]]:
24 """Check if header is a sequence of RTFColumnHeader instances."""
25 return isinstance(header, (list, tuple))
28def is_list_body(body: RTFBody | list[RTFBody] | None) -> TypeGuard[list[RTFBody]]:
29 """Check if body is a list of RTFBody instances."""
30 return isinstance(body, list)
33def is_nested_header_list(
34 header: Any,
35) -> TypeGuard[list[list[RTFColumnHeader | None]]]:
36 """Check if header is a nested list of RTFColumnHeader instances."""
37 return isinstance(header, list) and len(header) > 0 and isinstance(header[0], list)
40def is_flat_header_list(
41 header: Any,
42) -> TypeGuard[list[RTFColumnHeader | None]]:
43 """Check if header is a flat list of RTFColumnHeader instances."""
44 return isinstance(header, list) and (
45 len(header) == 0 or not isinstance(header[0], list)
46 )