Validator for int/float ranges. `min_val` and `max_val` are inclusive.
| 97 | |
| 98 | |
| 99 | class RangeValue(ValidatedValue): |
| 100 | """Validator for int/float ranges. `min_val` and `max_val` are inclusive.""" |
| 101 | |
| 102 | def __init__( |
| 103 | self, |
| 104 | value: int | float, |
| 105 | min_val: int | float, |
| 106 | max_val: int | float, |
| 107 | ): |
| 108 | if value < min_val or value > max_val: |
| 109 | # min and max are inclusive. |
| 110 | raise ValueError() |
| 111 | self._value = value |
| 112 | self._min_val = min_val |
| 113 | self._max_val = max_val |
| 114 | |
| 115 | @property |
| 116 | def value(self) -> int | float: |
| 117 | return self._value |
| 118 | |
| 119 | @property |
| 120 | def min_val(self) -> int | float: |
| 121 | """Minimum value of the range.""" |
| 122 | return self._min_val |
| 123 | |
| 124 | @property |
| 125 | def max_val(self) -> int | float: |
| 126 | """Maximum value of the range.""" |
| 127 | return self._max_val |
| 128 | |
| 129 | @property |
| 130 | def click_range(self) -> "click.IntRange | click.FloatRange": |
| 131 | """A `click` parameter type matching this range's bounds and value type.""" |
| 132 | if isinstance(self._value, int): |
| 133 | return click.IntRange(int(self._min_val), int(self._max_val)) |
| 134 | return click.FloatRange(float(self._min_val), float(self._max_val)) |
| 135 | |
| 136 | @staticmethod |
| 137 | def from_config(config_value: str, default: "RangeValue") -> "RangeValue": |
| 138 | try: |
| 139 | return RangeValue( |
| 140 | value=int(config_value) if isinstance(default.value, int) else float(config_value), |
| 141 | min_val=default.min_val, |
| 142 | max_val=default.max_val, |
| 143 | ) |
| 144 | except ValueError as ex: |
| 145 | raise OptionParseFailure( |
| 146 | f"Value must be between {default.min_val} and {default.max_val}." |
| 147 | ) from ex |
| 148 | |
| 149 | |
| 150 | class CropValue(ValidatedValue): |
no outgoing calls
no test coverage detected