Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions mollie/api/objects/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ def __getitem__(self, key):
return self.object_type(item, self.client)

if isinstance(key, slice):
_start = key.start or 0
_stop = key.stop or self["count"]
_step = key.step or 1
# slice.indices() correctly resolves None, negative and out-of-range
# bounds. The previous ``key.stop or self["count"]`` treated a stop
# of 0 as "unset", so e.g. ``obj_list[:0]`` returned the whole list
# instead of an empty one (and negative indices were mishandled).
_start, _stop, _step = key.indices(len(self))
sliced_data = [self["_embedded"][object_name][x] for x in range(_start, _stop, _step)]
# Now we mock a result based on the sliced data
sliced_result = {
Expand Down
6 changes: 6 additions & 0 deletions tests/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,9 @@ def test_list_supports_slice_sequences(client, response):
slice_step_only = methods[::3]
assert_list_object(slice_step_only, Method, 4), "Slicing with only a step value should be possible"
assert [x.id for x in slice_step_only] == ["ideal", "bancontact", "kbc", "giftcard"]

slice_empty = methods[:0]
assert [x.id for x in slice_empty] == [], "A slice with a stop of 0 should be empty"

slice_negative = methods[-2:]
assert [x.id for x in slice_negative] == ["inghomepay", "giftcard"], "Negative slicing should be possible"