MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / _getitem

Function _getitem

lib/sqlalchemy/orm/util.py:2143–2179  ·  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

2141
2142
2143def _getitem(iterable_query: Query[Any], item: Any) -> Any:
2144 """calculate __getitem__ in terms of an iterable query object
2145 that also has a slice() method.
2146
2147 """
2148
2149 def _no_negative_indexes():
2150 raise IndexError(
2151 "negative indexes are not accepted by SQL "
2152 "index / slice operators"
2153 )
2154
2155 if isinstance(item, slice):
2156 start, stop, step = util.decode_slice(item)
2157
2158 if (
2159 isinstance(stop, int)
2160 and isinstance(start, int)
2161 and stop - start <= 0
2162 ):
2163 return []
2164
2165 elif (isinstance(start, int) and start < 0) or (
2166 isinstance(stop, int) and stop < 0
2167 ):
2168 _no_negative_indexes()
2169
2170 res = iterable_query.slice(start, stop)
2171 if step is not None:
2172 return list(res)[None : None : item.step]
2173 else:
2174 return list(res)
2175 else:
2176 if item == -1:
2177 _no_negative_indexes()
2178 else:
2179 return list(iterable_query[item : item + 1])[0]
2180
2181
2182def _is_mapped_annotation(

Callers

nothing calls this directly

Calls 2

_no_negative_indexesFunction · 0.85
sliceMethod · 0.45

Tested by

no test coverage detected