Skip to content

Commit

Permalink
Merge pull request ipython#2311 from minrk/trait_change
Browse files Browse the repository at this point in the history
always perform requested trait assignments

Some assignments do not change value, but do change the object,
and these should not be ignored.

For instance, container objects are currently impossible to assign
if they evaluate as equal, even if their types don't match
(e.g. assigning an empty defaultdict to a Dict trait, which prompted this PR).
  • Loading branch information
minrk committed Sep 11, 2012
2 parents 8ffe6c6 + 3e41008 commit cfc63db
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 13 additions & 1 deletion IPython/utils/tests/test_traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
import sys
from unittest import TestCase

import nose.tools as nt
from nose import SkipTest

from IPython.utils.traitlets import (
HasTraits, MetaHasTraits, TraitType, Any, CBytes,
HasTraits, MetaHasTraits, TraitType, Any, CBytes, Dict,
Int, Long, Integer, Float, Complex, Bytes, Unicode, TraitError,
Undefined, Type, This, Instance, TCPAddress, List, Tuple,
ObjectName, DottedObjectName, CRegExp
Expand Down Expand Up @@ -906,3 +907,14 @@ def coerce(self, value):
_default_value = re.compile(r'')
_good_values = [r'\d+', re.compile(r'\d+')]
_bad_values = [r'(', None, ()]

class DictTrait(HasTraits):
value = Dict()

def test_dict_assignment():
d = dict()
c = DictTrait()
c.value = d
d['a'] = 5
nt.assert_equal(d, c.value)
nt.assert_true(c.value is d)
2 changes: 1 addition & 1 deletion IPython/utils/traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ def __get__(self, obj, cls=None):
def __set__(self, obj, value):
new_value = self._validate(obj, value)
old_value = self.__get__(obj)
obj._trait_values[self.name] = new_value
if old_value != new_value:
obj._trait_values[self.name] = new_value
obj._notify_trait(self.name, old_value, new_value)

def _validate(self, obj, value):
Expand Down

0 comments on commit cfc63db

Please sign in to comment.