MCPcopy Index your code
hub / github.com/sqlalchemy/sqlalchemy / _getitem

Function _getitem

lib/sqlalchemy/orm/util.py:2183–2219  ·  view source on GitHub ↗

calculate __getitem__ in terms of an iterable query object that also has a slice() method.

(iterable_query: Query[Any], item: Any)

Source from the content-addressed store, hash-verified

2181
2182
2183def _getitem(iterable_query: Query[Any], item: Any) -> Any:
2184 """calculate __getitem__ in terms of an iterable query object
2185 that also has a slice() method.
2186
2187 """
2188
2189 def _no_negative_indexes():
2190 raise IndexError(
2191 "negative indexes are not accepted by SQL "
2192 "index / slice operators"
2193 )
2194
2195 if isinstance(item, slice):
2196 start, stop, step = util.decode_slice(item)
2197
2198 if (
2199 isinstance(stop, int)
2200 and isinstance(start, int)
2201 and stop - start <= 0
2202 ):
2203 return []
2204
2205 elif (isinstance(start, int) and start < 0) or (
2206 isinstance(stop, int) and stop < 0
2207 ):
2208 _no_negative_indexes()
2209
2210 res = iterable_query.slice(start, stop)
2211 if step is not None:
2212 return list(res)[None : None : item.step]
2213 else:
2214 return list(res)
2215 else:
2216 if item == -1:
2217 _no_negative_indexes()
2218 else:
2219 return list(iterable_query[item : item + 1])[0]
2220
2221
2222def _is_mapped_annotation(

Callers

nothing calls this directly

Calls 2

_no_negative_indexesFunction · 0.85
sliceMethod · 0.45

Tested by

no test coverage detected