Handles ``Model.objects[A:B]`` with both bounds set. Mixed-sign bounds (e.g. ``[3:-2]``) need a ``COUNT(*)`` to resolve so they are rejected; otherwise the sign of the bounds picks the forward or reverse variant. Examples:: [2:8] → _forward_range(2, 8) [-5:-2] →
(start: int, stop: int)
| 233 | |
| 234 | |
| 235 | def _slice_range(start: int, stop: int) -> SliceBounds: |
| 236 | """ |
| 237 | Handles ``Model.objects[A:B]`` with both bounds set. Mixed-sign bounds |
| 238 | (e.g. ``[3:-2]``) need a ``COUNT(*)`` to resolve so they are rejected; |
| 239 | otherwise the sign of the bounds picks the forward or reverse variant. |
| 240 | |
| 241 | Examples:: |
| 242 | |
| 243 | [2:8] → _forward_range(2, 8) |
| 244 | [-5:-2] → _reverse_range(-5, -2) |
| 245 | [3:-2] → raises QueryDefinitionError (mixed sign) |
| 246 | |
| 247 | :param start: lower bound from the slice |
| 248 | :type start: int |
| 249 | :param stop: upper bound from the slice |
| 250 | :type stop: int |
| 251 | :return: normalized slice parameters |
| 252 | :rtype: SliceBounds |
| 253 | """ |
| 254 | start_neg = start < 0 |
| 255 | stop_neg = stop < 0 |
| 256 | if start_neg != stop_neg: |
| 257 | raise QueryDefinitionError( |
| 258 | "Mixed positive and negative slice bounds are not supported " |
| 259 | "without an explicit count; use .count() with " |
| 260 | ".offset()/.limit() instead." |
| 261 | ) |
| 262 | if start_neg: |
| 263 | return _reverse_range(start, stop) |
| 264 | return _forward_range(start, stop) |
| 265 | |
| 266 | |
| 267 | def _forward_range(start: int, stop: int) -> SliceBounds: |
no test coverage detected