diff --git a/traits/tests/test_trait_list_object.py b/traits/tests/test_trait_list_object.py index d275346bb..23374a6f1 100644 --- a/traits/tests/test_trait_list_object.py +++ b/traits/tests/test_trait_list_object.py @@ -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]) @@ -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, diff --git a/traits/tests/test_traits.py b/traits/tests/test_traits.py index 6ab0c40b4..e92951384 100644 --- a/traits/tests/test_traits.py +++ b/traits/tests/test_traits.py @@ -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( diff --git a/traits/trait_list_object.py b/traits/trait_list_object.py index 3a857c248..ec6f7ffea 100644 --- a/traits/trait_list_object.py +++ b/traits/trait_list_object.py @@ -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):