Skip to content

Commit

Permalink
Fix missing observer registration when inserting into nested list (#1761
Browse files Browse the repository at this point in the history
)

This PR fixes the behaviour of `TraitList.insert`, where the item in the
notification event didn't match the item actually added to the this.

Thanks Robert Haschke for the discovery, diagnosis and  fix.
---------

Co-authored-by: Mark Dickinson <[email protected]>
  • Loading branch information
rhaschke and mdickinson authored Dec 7, 2023
1 parent 3c6b250 commit 2b95e24
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 24 additions & 0 deletions traits/tests/test_observe.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,3 +906,27 @@ class Derived(Base):
self.assertEqual(len(events), 0)
obj.bar = 5
self.assertEqual(len(events), 1)

def test_list_of_lists_insert(self):
# Regression test for enthought/traits#1761

# Given a list-of-lists trait, and an observer for the items
# on the inner list.

class A(HasTraits):
foo = List(List(Int()))

obj = A()
events = []
obj.observe(events.append, "foo:items:items")

# When we insert a list, then modify an item in that list
obj.foo.insert(0, [1, 2, 3])
obj.foo[0][2] = 4

# Then we get the expected event.
self.assertEqual(len(events), 1)
event = events[0]
self.assertEqual(event.index, 2)
self.assertEqual(event.removed, [3])
self.assertEqual(event.added, [4])
2 changes: 1 addition & 1 deletion traits/trait_list_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def insert(self, index, object):
else:
normalized_index = min(index, len(self))
object = self.item_validator(object)
super().insert(index, self.item_validator(object))
super().insert(index, object)
self.notify(normalized_index, [], [object])

def pop(self, index=-1):
Expand Down

0 comments on commit 2b95e24

Please sign in to comment.