Coverage for src/rtflite/type_guards.py: 79%
14 statements
« prev ^ index » next coverage.py v7.10.3, created at 2025-08-14 16:35 +0000
« prev ^ index » next coverage.py v7.10.3, created at 2025-08-14 16:35 +0000
1"""Type guards for RTF components to handle Union types safely."""
3from typing import Any, TypeGuard
5from .input import RTFBody, RTFColumnHeader
8def is_single_header(
9 header: RTFColumnHeader | list[RTFColumnHeader | None] | None,
10) -> TypeGuard[RTFColumnHeader]:
11 """Check if header is a single RTFColumnHeader instance."""
12 return header is not None and not isinstance(header, list)
15def is_single_body(body: RTFBody | list[RTFBody] | None) -> TypeGuard[RTFBody]:
16 """Check if body is a single RTFBody instance."""
17 return body is not None and not isinstance(body, list)
20def is_list_header(
21 header: RTFColumnHeader | list[RTFColumnHeader | None] | None,
22) -> TypeGuard[list[RTFColumnHeader | None]]:
23 """Check if header is a list of RTFColumnHeader instances."""
24 return isinstance(header, list)
27def is_list_body(body: RTFBody | list[RTFBody] | None) -> TypeGuard[list[RTFBody]]:
28 """Check if body is a list of RTFBody instances."""
29 return isinstance(body, list)
32def is_nested_header_list(
33 header: Any,
34) -> TypeGuard[list[list[RTFColumnHeader | None]]]:
35 """Check if header is a nested list of RTFColumnHeader instances."""
36 return isinstance(header, list) and len(header) > 0 and isinstance(header[0], list)
39def is_flat_header_list(
40 header: Any,
41) -> TypeGuard[list[RTFColumnHeader | None]]:
42 """Check if header is a flat list of RTFColumnHeader instances."""
43 return isinstance(header, list) and (
44 len(header) == 0 or not isinstance(header[0], list)
45 )