Coverage for src / local_deep_research / utilities / type_utils.py: 100%

9 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-02-25 01:07 +0000

1""" 

2Type conversion utilities. 

3 

4This module provides type conversion functions that are used throughout 

5the codebase. It is intentionally kept free of internal dependencies to 

6avoid circular import issues. 

7""" 

8 

9from typing import Any 

10 

11 

12def to_bool(value: Any, default: bool = False) -> bool: 

13 """ 

14 Convert a value to boolean, handling string representations. 

15 

16 This is a standalone utility for converting any value to boolean, 

17 centralizing the string-to-boolean conversion logic that was 

18 previously scattered throughout the codebase. 

19 

20 Handles truthy string representations that may come from: 

21 - API requests 

22 - Configuration files 

23 - SQLite (which lacks native boolean type) 

24 - Environment variables 

25 

26 Args: 

27 value: The value to convert 

28 default: Default boolean if value is None 

29 

30 Returns: 

31 Boolean value 

32 

33 Examples: 

34 >>> to_bool("true") 

35 True 

36 >>> to_bool("yes") 

37 True 

38 >>> to_bool("1") 

39 True 

40 >>> to_bool("false") 

41 False 

42 >>> to_bool(1) 

43 True 

44 >>> to_bool(None, default=True) 

45 True 

46 """ 

47 if isinstance(value, bool): 

48 return value 

49 if isinstance(value, str): 

50 # Use strip() to handle whitespace that often appears in env vars 

51 # e.g., from shell parsing, config files, or copy-paste errors 

52 return value.strip().lower() in ("true", "1", "yes", "on", "enabled") 

53 if value is None: 

54 return default 

55 # For other types (int, etc.), use Python's bool conversion 

56 return bool(value)