diff --git a/IPython/utils/tests/test_traitlets.py b/IPython/utils/tests/test_traitlets.py index f2823b2..f9de690 100644 --- a/IPython/utils/tests/test_traitlets.py +++ b/IPython/utils/tests/test_traitlets.py @@ -46,7 +46,7 @@ class HasTraitletsStub(object): self._traitlet_values = {} self._traitlet_notifiers = {} - def _notify(self, name, old, new): + def _notify_traitlet(self, name, old, new): self._notify_name = name self._notify_old = old self._notify_new = new @@ -321,6 +321,15 @@ class TestAddTraitlet(TestCase): self.assertEquals(a.b,10.0) self.assertRaises(TraitletError, setattr, a, 'b', 'bad value') +class TestTraitletKeys(TestCase): + + def test_keys(self): + class A(HasTraitlets): + a = Int + b = Float + a = A() + self.assertEquals(a.traitlet_keys(),['a','b']) + #----------------------------------------------------------------------------- # Tests for specific traitlet types diff --git a/IPython/utils/traitlets.py b/IPython/utils/traitlets.py index 4274bb2..07c2929 100644 --- a/IPython/utils/traitlets.py +++ b/IPython/utils/traitlets.py @@ -16,9 +16,15 @@ We don't support: * Delegation * Automatic GUI generation -* A full set of trait types +* A full set of trait types. Most importantly, we don't provide container + traitlets (list, dict, tuple) that can trigger notifications if their + contents change. * API compatibility with enthought.traits +There are also some important difference in our design: + +* enthought.traits does not validate default values. We do. + We choose to create this module because we need these capabilities, but we need them to be pure Python so they work in all Python implementations, including Jython and IronPython. @@ -184,7 +190,7 @@ class TraitletType(object): old_value = self.__get__(obj) if old_value != new_value: obj._traitlet_values[self.name] = new_value - obj._notify(self.name, old_value, new_value) + obj._notify_traitlet(self.name, old_value, new_value) else: obj._traitlet_values[self.name] = new_value @@ -248,7 +254,7 @@ class HasTraitlets(object): self._traitlet_values = {} self._traitlet_notifiers = {} - def _notify(self, name, old_value, new_value): + def _notify_traitlet(self, name, old_value, new_value): # First dynamic ones callables = self._traitlet_notifiers.get(name,[]) @@ -368,6 +374,10 @@ class HasTraitlets(object): inst.name = name setattr(self.__class__, name, inst) + def traitlet_keys(self): + """Get a list of all the names of this classes traitlets.""" + return [memb[0] for memb in inspect.getmembers(self.__class__) if isinstance(memb[1], TraitletType)] + #----------------------------------------------------------------------------- # Actual TraitletTypes implementations/subclasses