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

Make TraitListObject notify even if list values stay the same #1026

Merged
merged 4 commits into from
Apr 24, 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
35 changes: 34 additions & 1 deletion traits/tests/test_trait_list_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def test_setitem_converts(self):
msg="Event contains non-integers for int-only list",
)

def test_setitem_nochange(self):
def test_setitem_no_structural_change(self):
tl = TraitList([1, 2, 3],
item_validator=int_item_validator,
notifiers=[self.notification_handler])
Expand All @@ -263,6 +263,39 @@ def test_setitem_nochange(self):
self.assertIsNone(self.removed)
self.assertIsNone(self.added)

def test_setitem_no_item_change(self):
tl = TraitList([1, 2, 3],
item_validator=int_item_validator,
notifiers=[self.notification_handler])

tl[0] = 1
self.assertEqual(tl, [1, 2, 3])
self.assertEqual(self.index, 0)
self.assertEqual(self.removed, [1])
self.assertEqual(self.added, [1])

def test_setitem_no_removed(self):
tl = TraitList([1, 2, 3],
item_validator=int_item_validator,
notifiers=[self.notification_handler])

tl[3:] = [4, 5, 6]
self.assertEqual(tl, [1, 2, 3, 4, 5, 6])
self.assertEqual(self.index, 3)
self.assertEqual(self.removed, [])
self.assertEqual(self.added, [4, 5, 6])

def test_setitem_no_added(self):
tl = TraitList([1, 2, 3],
item_validator=int_item_validator,
notifiers=[self.notification_handler])

tl[1:2] = []
self.assertEqual(tl, [1, 3])
self.assertEqual(self.index, 1)
self.assertEqual(self.removed, [2])
self.assertEqual(self.added, [])

def test_setitem_iterable(self):
tl = TraitList([1, 2, 3],
item_validator=int_item_validator,
Expand Down
7 changes: 5 additions & 2 deletions traits/tests/test_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -1089,9 +1089,12 @@ def test_trait_list_event(self):
self.assertLastTraitListEventEqual(0, [6, 7], [4, 5])
self.obj.alist[0:2:1] = [8, 9]
self.assertLastTraitListEventEqual(0, [4, 5], [8, 9])
old_event = self.last_event
self.obj.alist[0:2:1] = [8, 9]
# If no values changed, no new TraitListEvent will be generated.
# If list values stay the same, a new TraitListEvent will be generated.
self.assertLastTraitListEventEqual(0, [8, 9], [8, 9])
old_event = self.last_event
self.obj.alist[4:] = []
# If no structural change, NO new TraitListEvent will be generated.
self.assertIs(self.last_event, old_event)
self.obj.alist[0:4:2] = [10, 11]
self.assertLastTraitListEventEqual(
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 @@ -285,7 +285,7 @@ def __setitem__(self, key, value):
pass
super().__setitem__(key, value)

if added != removed:
if added or removed:
self.notify(normalized_index, removed, added)

def append(self, object):
Expand Down