Skip to content

Commit

Permalink
Merge pull request #43 from SylvainCorlay/on_trait_change_signature
Browse files Browse the repository at this point in the history
Add HasTraits instance to the signature of trait changed handler
  • Loading branch information
minrk committed Jul 19, 2015
2 parents 75d3b2a + a42806d commit 5f0a9a4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
7 changes: 7 additions & 0 deletions traitlets/tests/test_traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ def callback2(name, new):
self.cb = (name, new)
def callback3(name, old, new):
self.cb = (name, old, new)
def callback4(name, old, new, obj):
self.cb = (name, old, new, obj)

class A(HasTraits):
a = Int
Expand All @@ -324,6 +326,11 @@ class A(HasTraits):
self.assertEqual(self.cb,('a',1000,10000))
a.on_trait_change(callback3, 'a', remove=True)

a.on_trait_change(callback4, 'a')
a.a = 100000
self.assertEqual(self.cb,('a',10000,100000,a))
a.on_trait_change(callback4, 'a', remove=True)

self.assertEqual(len(a._trait_notifiers['a']),0)

def test_notify_only_once(self):
Expand Down
8 changes: 5 additions & 3 deletions traitlets/traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,9 +681,11 @@ def _notify_trait(self, name, old_value, new_value):
c(name, new_value)
elif nargs + offset == 3:
c(name, old_value, new_value)
elif nargs + offset == 4:
c(name, old_value, new_value, self)
else:
raise TraitError('a trait changed callback '
'must have 0-3 arguments.')
'must have 0-4 arguments.')
else:
raise TraitError('a trait changed callback '
'must be callable.')
Expand Down Expand Up @@ -719,8 +721,8 @@ def on_trait_change(self, handler, name=None, remove=False):
----------
handler : callable
A callable that is called when a trait changes. Its
signature can be handler(), handler(name), handler(name, new)
or handler(name, old, new).
signature can be handler(), handler(name), handler(name, new),
handler(name, old, new), or handler(name, old, new, self).
name : list, str, None
If None, the handler will apply to all traits. If a list
of str, handler will apply to all names in the list. If a
Expand Down

0 comments on commit 5f0a9a4

Please sign in to comment.