Skip to content

Commit

Permalink
Merge f3ef826 into 0ec0cc7
Browse files Browse the repository at this point in the history
  • Loading branch information
tuomas2 committed Jan 7, 2015
2 parents 0ec0cc7 + f3ef826 commit d7cae18
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
36 changes: 35 additions & 1 deletion traits/tests/test_property_notifications.py
Expand Up @@ -14,7 +14,7 @@

from __future__ import absolute_import

from ..api import HasTraits, Property
from ..api import HasTraits, Property, List, Int, cached_property


class Test(HasTraits):
Expand Down Expand Up @@ -81,3 +81,37 @@ def test_property_notifications():
test_7.on_trait_change(on_value_changed, 'value')
test_7.on_trait_change(on_anyvalue_changed)
test_7.value = 'test 7'


class Entity(HasTraits):
val = Int

class WrongEntity(HasTraits):
val2 = Int

class WithList(HasTraits):
mylist = List
prop = Property(depends_on='mylist[], mylist.val')

@cached_property
def _get_prop(self):
return sum(i.val for i in self.mylist if isinstance(i, Entity))

def test_property_notification_with_lists():
v1 = Entity(val=1)
v2 = Entity(val=2)
sl = WithList()
assert sl.prop == 0

sl.mylist = [v1, v2]
assert sl.prop == 3

v3 = WrongEntity(val2=3) #Entity without proper attribute should not trigger property notification
sl.mylist.append(v3)
assert sl.prop == 3

sl.mylist = [v1,v2, v3, 2] #please do not mind if non-HasTraits object is in list
assert sl.prop == 3

sl.mylist.append(v1)
assert sl.prop == 4
4 changes: 2 additions & 2 deletions traits/traits_listener.py
Expand Up @@ -32,7 +32,7 @@
from string import whitespace
from types import MethodType

from .has_traits import HasPrivateTraits
from .has_traits import HasPrivateTraits, HasTraits
from .trait_base import Undefined, Uninitialized
from .traits import Property
from .trait_types import Str, Int, Bool, Instance, List, Enum, Any
Expand Down Expand Up @@ -341,7 +341,7 @@ def register ( self, new ):
"""
# Make sure we actually have an object to set listeners on and that it
# has not already been registered (cycle breaking):
if (new is None) or (new is Undefined) or (new in self.active):
if not isinstance(new, HasTraits) or new in self.active:
return INVALID_DESTINATION

# Create a dictionary of {name: trait_values} that match the object's
Expand Down

0 comments on commit d7cae18

Please sign in to comment.