@@ -34,7 +34,7 @@ def __init__(
3434 self ._results = None
3535 self ._limit = self ._client .default_limit or 100
3636 self ._offset = 0
37- self ._slice = False
37+ self ._slice = None
3838 self ._content_range = None
3939 self ._fields = None
4040 self ._search = None
@@ -264,6 +264,21 @@ def _copy(self):
264264
265265 return rs
266266
267+ def _validate_key (self , key ):
268+ if not isinstance (key , (int , slice )):
269+ raise TypeError ('ResourceSet indices must be integers or slices.' )
270+
271+ if isinstance (key , slice ) and (key .start is None or key .stop is None ):
272+ raise ValueError ('Both start and stop indexes must be specified.' )
273+
274+ if (not isinstance (key , slice ) and (key < 0 )) or (
275+ isinstance (key , slice ) and (key .start < 0 or key .stop < 0 )
276+ ):
277+ raise ValueError ('Negative indexing is not supported.' )
278+
279+ if isinstance (key , slice ) and not (key .step is None or key .step == 0 ):
280+ raise ValueError ('Indexing with step is not supported.' )
281+
267282 def help (self ):
268283 """
269284 Output the ResourceSet documentation to the console.
@@ -301,7 +316,7 @@ def __bool__(self):
301316
302317 def __getitem__ (self , key ): # noqa: CCR001
303318 """
304- If called with and integer index, returns the item
319+ If called with slice and integer index, returns the item
305320 at index ``key``.
306321
307322 If key is a slice, set the pagination limit and offset
@@ -313,19 +328,7 @@ def __getitem__(self, key): # noqa: CCR001
313328 :return: The resource at index ``key`` or self if ``key`` is a slice.
314329 :rtype: dict, ResultSet
315330 """
316- if not isinstance (key , (int , slice )):
317- raise TypeError ('ResourceSet indices must be integers or slices.' )
318-
319- if isinstance (key , slice ) and (key .start is None or key .stop is None ):
320- raise ValueError ('Both start and stop indexes must be specified.' )
321-
322- if (not isinstance (key , slice ) and (key < 0 )) or (
323- isinstance (key , slice ) and (key .start < 0 or key .stop < 0 )
324- ):
325- raise ValueError ('Negative indexing is not supported.' )
326-
327- if isinstance (key , slice ) and not (key .step is None or key .step == 0 ):
328- raise ValueError ('Indexing with step is not supported.' )
331+ self ._validate_key (key )
329332
330333 if self ._results is not None :
331334 return self ._results [key ]
@@ -339,8 +342,10 @@ def __getitem__(self, key): # noqa: CCR001
339342
340343 copy = self ._copy ()
341344 copy ._offset = key .start
342- copy ._limit = key .stop - key .start
343- copy ._slice = True
345+ copy ._slice = key
346+ if copy ._slice .stop - copy ._slice .start < copy ._limit :
347+ copy ._limit = copy ._slice .stop - copy ._slice .start
348+
344349 return copy
345350
346351 def count (self ):
@@ -429,7 +434,7 @@ def __bool__(self):
429434
430435 def __getitem__ (self , key ): # noqa: CCR001
431436 """
432- If called with and integer index, returns the item
437+ If called with slice and integer index, returns the item
433438 at index ``key``.
434439
435440 If key is a slice, set the pagination limit and offset
@@ -441,19 +446,7 @@ def __getitem__(self, key): # noqa: CCR001
441446 :return: The resource at index ``key`` or self if ``key`` is a slice.
442447 :rtype: dict, ResultSet
443448 """
444- if not isinstance (key , (int , slice )):
445- raise TypeError ('ResourceSet indices must be integers or slices.' )
446-
447- if isinstance (key , slice ) and (key .start is None or key .stop is None ):
448- raise ValueError ('Both start and stop indexes must be specified.' )
449-
450- if (not isinstance (key , slice ) and (key < 0 )) or (
451- isinstance (key , slice ) and (key .start < 0 or key .stop < 0 )
452- ):
453- raise ValueError ('Negative indexing is not supported.' )
454-
455- if isinstance (key , slice ) and not (key .step is None or key .step == 0 ):
456- raise ValueError ('Indexing with step is not supported.' )
449+ self ._validate_key (key )
457450
458451 if self ._results is not None :
459452 return self ._results [key ]
@@ -463,8 +456,10 @@ def __getitem__(self, key): # noqa: CCR001
463456
464457 copy = self ._copy ()
465458 copy ._offset = key .start
466- copy ._limit = key .stop - key .start
467- copy ._slice = True
459+ copy ._slice = key
460+ if copy ._slice .stop - copy ._slice .start < copy ._limit :
461+ copy ._limit = copy ._slice .stop - copy ._slice .start
462+
468463 return copy
469464
470465 async def count (self ):
0 commit comments