Skip to content

Commit

Permalink
Merge pull request #38 from SylvainCorlay/has_trait
Browse files Browse the repository at this point in the history
has_trait function
  • Loading branch information
takluyver committed Jun 16, 2015
2 parents a15ac67 + e0838f2 commit 4f23da2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/source/using_traitlets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Any class with traitlet attributes must inherit from :class:`HasTraits`.

.. autoclass:: HasTraits

.. automethod:: has_trait

.. automethod:: trait_names

.. automethod:: class_trait_names
Expand Down
2 changes: 2 additions & 0 deletions traitlets/tests/test_traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ class A(HasTraits):
a = A()
self.assertEqual(sorted(a.trait_names()),['f','i'])
self.assertEqual(sorted(A.class_trait_names()),['f','i'])
self.assertTrue(a.has_trait('f'))
self.assertFalse(a.has_trait('g'))

def test_trait_metadata(self):
class A(HasTraits):
Expand Down
5 changes: 4 additions & 1 deletion traitlets/traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,6 @@ def _notify_trait(self, name, old_value, new_value):
raise TraitError('a trait changed callback '
'must be callable.')


def _add_notifiers(self, handler, name):
if name not in self._trait_notifiers:
nlist = []
Expand Down Expand Up @@ -801,6 +800,10 @@ def class_own_traits(cls, **metadata):
return {n: t for (n, t) in cls.class_traits(**metadata).items()
if getattr(sup, n, None) is not t}

def has_trait(self, name):
"""Returns True if the object has a trait with the specified name."""
return isinstance(getattr(self.__class__, name, None), TraitType)

def trait_names(self, **metadata):
"""Get a list of all the names of this class' traits."""
return self.traits(**metadata).keys()
Expand Down

0 comments on commit 4f23da2

Please sign in to comment.