Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect event index for slice with stop < start #999

Merged
merged 1 commit into from
Apr 9, 2020
Merged
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
16 changes: 16 additions & 0 deletions traits/tests/test_trait_list_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,22 @@ def test_setitem_item_validation_failure(self):
foo.at_least_two[2:] = [5.0, 6.0]
self.assertEqual(foo.at_least_two, [1, 2, 3, 4])

def test_setitem_stop_lt_start(self):
# Regression test for enthought/traits#994.
events = []
foo = HasLengthConstrainedLists(at_least_two=[1, 2, 3, 4])
foo.on_trait_change(
lambda event: events.append(event), "at_least_two_items")

# Note: items are inserted at position 4, not position 2.
foo.at_least_two[4:2] = [5, 6, 7]

self.assertEqual(len(events), 1)
event = events[0]
self.assertEqual(event.index, 4)
self.assertEqual(event.removed, [])
self.assertEqual(event.added, [5, 6, 7])

def test_append(self):
foo = HasLengthConstrainedLists(at_most_five=[1, 2, 3])
foo.at_most_five.append(6)
Expand Down
2 changes: 1 addition & 1 deletion traits/trait_list_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ def notifier(self, trait_list, index, removed, added):
# bug-for-bug conversion of parameters to TraitListEvent
if isinstance(index, slice):
if index.step is None or index.step == 1:
index = min(index.start, index.stop)
index = index.start

event = TraitListEvent(index, removed, added)
items_event = self.trait.items_event()
Expand Down