Top-level dispatcher: turns a Python integer index or slice into a :class:`SliceBounds` suitable for ``QuerySet``. Delegates to a dedicated helper for each shape. Any shape that would require a ``COUNT(*)`` round-trip (``step != 1``, a bare ``[:-N]``, or mixed-sign bounds) rais
(key: Union[int, slice])
| 101 | |
| 102 | |
| 103 | def normalize_slice(key: Union[int, slice]) -> SliceBounds: |
| 104 | """ |
| 105 | Top-level dispatcher: turns a Python integer index or slice into a |
| 106 | :class:`SliceBounds` suitable for ``QuerySet``. |
| 107 | |
| 108 | Delegates to a dedicated helper for each shape. Any shape that would |
| 109 | require a ``COUNT(*)`` round-trip (``step != 1``, a bare ``[:-N]``, or |
| 110 | mixed-sign bounds) raises ``QueryDefinitionError``. |
| 111 | |
| 112 | Examples:: |
| 113 | |
| 114 | 5 → SliceBounds(limit=1, offset=5, reverse=False) |
| 115 | slice(2, 8) → SliceBounds(limit=6, offset=2, reverse=False) |
| 116 | slice(-3, None) → SliceBounds(limit=3, offset=0, reverse=True) |
| 117 | |
| 118 | :param key: integer or slice passed to ``QuerySet.__getitem__`` |
| 119 | :type key: int | slice |
| 120 | :return: normalized slice parameters |
| 121 | :rtype: SliceBounds |
| 122 | """ |
| 123 | if isinstance(key, int): |
| 124 | return _int_to_limit_offset(key) |
| 125 | if isinstance(key, slice): |
| 126 | return _slice_to_limit_offset(key) |
| 127 | raise QueryDefinitionError( |
| 128 | f"QuerySet indices must be integers or slices, not {type(key).__name__}." |
| 129 | ) |
| 130 | |
| 131 | |
| 132 | def _int_to_limit_offset(key: int) -> SliceBounds: |