diff --git a/mollie/api/objects/list.py b/mollie/api/objects/list.py index b8fd3f46..07822e96 100644 --- a/mollie/api/objects/list.py +++ b/mollie/api/objects/list.py @@ -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 = { diff --git a/tests/test_list.py b/tests/test_list.py index 29db795a..5e07bf5d 100644 --- a/tests/test_list.py +++ b/tests/test_list.py @@ -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"