Compare two range bounds. Return -1, 0 or 1 respectively when `value1` is less than, equal to or greater than `value2`. When `only_value` is ``True``, do not consider the *inclusivity* of the edges, just their values.
(
self,
value1: Optional[_T],
bound1: str,
value2: Optional[_T],
bound2: str,
only_values: bool = False,
)
| 177 | return None |
| 178 | |
| 179 | def _compare_edges( |
| 180 | self, |
| 181 | value1: Optional[_T], |
| 182 | bound1: str, |
| 183 | value2: Optional[_T], |
| 184 | bound2: str, |
| 185 | only_values: bool = False, |
| 186 | ) -> int: |
| 187 | """Compare two range bounds. |
| 188 | |
| 189 | Return -1, 0 or 1 respectively when `value1` is less than, |
| 190 | equal to or greater than `value2`. |
| 191 | |
| 192 | When `only_value` is ``True``, do not consider the *inclusivity* |
| 193 | of the edges, just their values. |
| 194 | """ |
| 195 | |
| 196 | value1_is_lower_bound = bound1 in {"[", "("} |
| 197 | value2_is_lower_bound = bound2 in {"[", "("} |
| 198 | |
| 199 | # Infinite edges are equal when they are on the same side, |
| 200 | # otherwise a lower edge is considered less than the upper end |
| 201 | if value1 is value2 is None: |
| 202 | if value1_is_lower_bound == value2_is_lower_bound: |
| 203 | return 0 |
| 204 | else: |
| 205 | return -1 if value1_is_lower_bound else 1 |
| 206 | elif value1 is None: |
| 207 | return -1 if value1_is_lower_bound else 1 |
| 208 | elif value2 is None: |
| 209 | return 1 if value2_is_lower_bound else -1 |
| 210 | |
| 211 | # Short path for trivial case |
| 212 | if bound1 == bound2 and value1 == value2: |
| 213 | return 0 |
| 214 | |
| 215 | value1_inc = bound1 in {"[", "]"} |
| 216 | value2_inc = bound2 in {"[", "]"} |
| 217 | step = self._get_discrete_step() |
| 218 | |
| 219 | if step is not None: |
| 220 | # "Normalize" the two edges as '[)', to simplify successive |
| 221 | # logic when the range is discrete: otherwise we would need |
| 222 | # to handle the comparison between ``(0`` and ``[1`` that |
| 223 | # are equal when dealing with integers while for floats the |
| 224 | # former is lesser than the latter |
| 225 | |
| 226 | if value1_is_lower_bound: |
| 227 | if not value1_inc: |
| 228 | value1 += step |
| 229 | value1_inc = True |
| 230 | else: |
| 231 | if value1_inc: |
| 232 | value1 += step |
| 233 | value1_inc = False |
| 234 | if value2_is_lower_bound: |
| 235 | if not value2_inc: |
| 236 | value2 += step |
no test coverage detected