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

1"""Type guards for RTF components to handle Union types safely.""" 

2 

3from typing import Any, TypeGuard 

4 

5from .input import RTFBody, RTFColumnHeader 

6 

7 

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) 

13 

14 

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) 

18 

19 

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) 

25 

26 

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) 

30 

31 

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) 

37 

38 

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 )