|
9 | 9 | from ._extremes import AbsMax, AbsMin |
10 | 10 | from .utils import validator |
11 | 11 |
|
12 | | -T = TypeVar("T", int, float, str, datetime) |
| 12 | +PossibleValueTypes = TypeVar("PossibleValueTypes", int, float, str, datetime) |
13 | 13 |
|
14 | 14 |
|
15 | 15 | @validator |
16 | 16 | def between( |
17 | | - value: T, |
| 17 | + value: PossibleValueTypes, |
18 | 18 | /, |
19 | 19 | *, |
20 | | - min_val: Union[T, AbsMin, None] = None, |
21 | | - max_val: Union[T, AbsMax, None] = None, |
| 20 | + min_val: Union[PossibleValueTypes, AbsMin, None] = None, |
| 21 | + max_val: Union[PossibleValueTypes, AbsMax, None] = None, |
22 | 22 | ): |
23 | 23 | """Validate that a number is between minimum and/or maximum value. |
24 | 24 |
|
25 | 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`_ |
| 26 | + not just integers. This validator is originally based on [WTForms-NumberRange-Validator][1]. |
27 | 27 |
|
28 | | - .. _WTForms-NumberRange-Validator: |
29 | | - https://github.com/wtforms/wtforms/blob/master/src/wtforms/validators.py#L166-L220 |
30 | | -
|
31 | | - Examples:: |
| 28 | + [1]: https://github.com/wtforms/wtforms/blob/master/src/wtforms/validators.py#L166-L220 |
32 | 29 |
|
| 30 | + Examples: |
33 | 31 | >>> from datetime import datetime |
34 | | -
|
35 | 32 | >>> between(5, min_val=2) |
36 | 33 | # Output: True |
37 | | -
|
38 | 34 | >>> between(13.2, min_val=13, max_val=14) |
39 | 35 | # Output: True |
40 | | -
|
41 | 36 | >>> between(500, max_val=400) |
42 | 37 | # Output: ValidationFailure(func=between, args=...) |
43 | | -
|
44 | 38 | >>> between( |
45 | 39 | ... datetime(2000, 11, 11), |
46 | 40 | ... min_val=datetime(1999, 11, 11) |
47 | 41 | ... ) |
48 | | - # True |
| 42 | + # Output: True |
49 | 43 |
|
50 | 44 | Args: |
51 | | - `value`: |
52 | | - [Required] Value which is to be compared. |
53 | | - `min_val`: |
54 | | - [Optional] The minimum required value of the number. |
| 45 | + value: |
| 46 | + Value which is to be compared. |
| 47 | + min_val: |
| 48 | + The minimum required value of the number. |
55 | 49 | If not provided, minimum value will not be checked. |
56 | | - `max_val`: |
57 | | - [Optional] The maximum value of the number. |
| 50 | + max_val: |
| 51 | + The maximum value of the number. |
58 | 52 | If not provided, maximum value will not be checked. |
59 | | - Either one of `min_val` or `max_val` must be provided. |
60 | 53 |
|
61 | 54 | Returns: |
62 | | - A `boolean` if `value` is greater than `min_val` and |
63 | | - less than `max_val`. |
| 55 | + (Literal[True]): |
| 56 | + If `value` is in between the given conditions. |
| 57 | + (ValidationFailure): |
| 58 | + If `value` is not in between the given conditions. |
64 | 59 |
|
65 | 60 | Raises: |
66 | | - `AssertionError`: |
67 | | - - If both `min_val` and `max_val` are `None`. |
68 | | - - If `min_val` is greater than `max_val`. |
| 61 | + AssertionError: 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. |
69 | 64 |
|
70 | | - `TypeError`: |
71 | | - - If there's a type mismatch before comparison |
| 65 | + Note: |
| 66 | + - `PossibleValueTypes` = `TypeVar("PossibleValueTypes", int, float, str, datetime)` |
| 67 | + - Either one of `min_val` or `max_val` must be provided. |
72 | 68 |
|
73 | | - .. versionadded:: 0.2 |
| 69 | + > *New in version 0.2.0*. |
74 | 70 | """ |
75 | 71 | if min_val is None and max_val is None: |
76 | 72 | raise AssertionError("At least one of either `min_val` or `max_val` must be specified") |
|
0 commit comments