MCPcopy
hub / github.com/ormar-orm/ormar / _slice_range

Function _slice_range

ormar/queryset/utils.py:235–264  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

233
234
235def _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
267def _forward_range(start: int, stop: int) -> SliceBounds:

Callers 1

_slice_to_limit_offsetFunction · 0.85

Calls 3

_reverse_rangeFunction · 0.85
_forward_rangeFunction · 0.85

Tested by

no test coverage detected