Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/validators/between.py: 60%

30 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:08 +0000

1"""Between.""" 

2# -*- coding: utf-8 -*- 

3 

4# standard 

5from typing import TypeVar, Union 

6from datetime import datetime 

7 

8# local 

9from ._extremes import AbsMin, AbsMax 

10from .utils import validator 

11 

12PossibleValueTypes = TypeVar("PossibleValueTypes", int, float, str, datetime) 

13 

14 

15@validator 

16def between( 

17 value: PossibleValueTypes, 

18 /, 

19 *, 

20 min_val: Union[PossibleValueTypes, AbsMin, None] = None, 

21 max_val: Union[PossibleValueTypes, AbsMax, None] = None, 

22): 

23 """Validate that a number is between minimum and/or maximum value. 

24 

25 This will work with any comparable type, such as floats, decimals and dates 

26 not just integers. This validator is originally based on [WTForms-NumberRange-Validator][1]. 

27 

28 [1]: https://github.com/wtforms/wtforms/blob/master/src/wtforms/validators.py#L166-L220 

29 

30 Examples: 

31 >>> from datetime import datetime 

32 >>> between(5, min_val=2) 

33 # Output: True 

34 >>> between(13.2, min_val=13, max_val=14) 

35 # Output: True 

36 >>> between(500, max_val=400) 

37 # Output: ValidationFailure(func=between, args=...) 

38 >>> between( 

39 ... datetime(2000, 11, 11), 

40 ... min_val=datetime(1999, 11, 11) 

41 ... ) 

42 # Output: True 

43 

44 Args: 

45 value: 

46 Value which is to be compared. 

47 min_val: 

48 The minimum required value of the number. 

49 If not provided, minimum value will not be checked. 

50 max_val: 

51 The maximum value of the number. 

52 If not provided, maximum value will not be checked. 

53 

54 Returns: 

55 (Literal[True]): 

56 If `value` is in between the given conditions. 

57 (ValidationFailure): 

58 If `value` is not in between the given conditions. 

59 

60 Raises: 

61 ValueError: If both `min_val` and `max_val` are `None`, 

62 or if `min_val` is greater than `max_val`. 

63 TypeError: If there's a type mismatch before comparison. 

64 

65 Note: 

66 - `PossibleValueTypes` = `TypeVar("PossibleValueTypes", int, float, str, datetime)` 

67 - Either one of `min_val` or `max_val` must be provided. 

68 

69 > *New in version 0.2.0*. 

70 """ 

71 if not value: 

72 return False 

73 

74 if min_val is None and max_val is None: 

75 raise ValueError("At least one of either `min_val` or `max_val` must be specified") 

76 

77 if max_val is None: 

78 max_val = AbsMax() 

79 if min_val is None: 

80 min_val = AbsMin() 

81 

82 if isinstance(min_val, AbsMin): 

83 if type(value) is type(max_val): 

84 return min_val <= value <= max_val 

85 raise TypeError("`value` and `max_val` must be of same type") 

86 

87 if isinstance(max_val, AbsMax): 

88 if type(value) is type(min_val): 

89 return min_val <= value <= max_val 

90 raise TypeError("`value` and `min_val` must be of same type") 

91 

92 if type(min_val) is type(max_val): 

93 if min_val > max_val: 

94 raise ValueError("`min_val` cannot be more than `max_val`") 

95 if type(value) is type(min_val): # or is type(max_val) 

96 return min_val <= value <= max_val 

97 raise TypeError("`value` and (`min_val` or `max_val`) must be of same type") 

98 

99 raise TypeError("`value` and `min_val` and `max_val` must be of same type")