Coverage for src / ezcompiler / utils / validators / type_validators.py: 47.37%

15 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-27 06:49 +0000

1# /////////////////////////////////////////////////////////////// 

2# TYPE_VALIDATORS - Type validation utilities 

3# Project: ezcompiler 

4# /////////////////////////////////////////////////////////////// 

5 

6""" 

7Type validators - Validation utilities for type checking. 

8 

9This module provides validation functions for checking data types and 

10validating integer values. 

11""" 

12 

13from __future__ import annotations 

14 

15# /////////////////////////////////////////////////////////////// 

16# IMPORTS 

17# /////////////////////////////////////////////////////////////// 

18# Standard library imports 

19from typing import Any 

20 

21# Local imports 

22from ...shared.exceptions.utils.validation_exceptions import TypeValidationError 

23 

24# /////////////////////////////////////////////////////////////// 

25# FUNCTIONS 

26# /////////////////////////////////////////////////////////////// 

27 

28 

29def validate_positive_integer(value: Any) -> bool: 

30 """ 

31 Validate that a value is a positive integer. 

32 

33 Args: 

34 value: Value to validate 

35 

36 Returns: 

37 bool: True if value is a positive integer, False otherwise 

38 

39 Example: 

40 >>> validate_positive_integer(5) 

41 True 

42 >>> validate_positive_integer(0) 

43 False 

44 >>> validate_positive_integer(-1) 

45 False 

46 """ 

47 return isinstance(value, int) and value > 0 

48 

49 

50def validate_non_negative_integer(value: Any) -> bool: 

51 """ 

52 Validate that a value is a non-negative integer. 

53 

54 Args: 

55 value: Value to validate 

56 

57 Returns: 

58 bool: True if value is a non-negative integer, False otherwise 

59 

60 Example: 

61 >>> validate_non_negative_integer(0) 

62 True 

63 >>> validate_non_negative_integer(5) 

64 True 

65 >>> validate_non_negative_integer(-1) 

66 False 

67 """ 

68 return isinstance(value, int) and value >= 0 

69 

70 

71def validate_boolean(value: Any) -> bool: 

72 """ 

73 Validate that a value is a boolean. 

74 

75 Args: 

76 value: Value to validate 

77 

78 Returns: 

79 bool: True if value is a boolean, False otherwise 

80 

81 Example: 

82 >>> validate_boolean(True) 

83 True 

84 >>> validate_boolean(False) 

85 True 

86 >>> validate_boolean(1) 

87 False 

88 """ 

89 return isinstance(value, bool) 

90 

91 

92def validate_type( 

93 value: Any, expected_type: type | tuple[type, ...], field_name: str = "Value" 

94) -> None: 

95 """ 

96 Validate that a value is of the expected type. 

97 

98 Args: 

99 value: Value to validate 

100 expected_type: Expected type or tuple of types 

101 field_name: Name of field for error messages 

102 

103 Raises: 

104 TypeValidationError: If value is not of expected type 

105 

106 Example: 

107 >>> validate_type("hello", str) 

108 >>> validate_type(42, int) 

109 >>> validate_type("hello", int) 

110 Traceback (most recent call last): 

111 ... 

112 TypeValidationError: Value must be of type int, got str 

113 """ 

114 if not isinstance(value, expected_type): 114 ↛ 115line 114 didn't jump to line 115 because the condition on line 114 was never true

115 if isinstance(expected_type, tuple): 

116 type_names = " or ".join(t.__name__ for t in expected_type) 

117 else: 

118 type_names = expected_type.__name__ 

119 raise TypeValidationError( 

120 f"{field_name} must be of type {type_names}, got {type(value).__name__}" 

121 )