##// END OF EJS Templates
Merge pull request #6331 from jasongrout/traitlet-metadata-default...
Brian E. Granger -
r17693:e25b123c merge
parent child Browse files
Show More
@@ -406,6 +406,13 class TestHasTraits(TestCase):
406 a = A()
406 a = A()
407 self.assertEqual(a.trait_metadata('i','config_key'), 'MY_VALUE')
407 self.assertEqual(a.trait_metadata('i','config_key'), 'MY_VALUE')
408
408
409 def test_trait_metadata_default(self):
410 class A(HasTraits):
411 i = Int()
412 a = A()
413 self.assertEqual(a.trait_metadata('i', 'config_key'), None)
414 self.assertEqual(a.trait_metadata('i', 'config_key', 'default'), 'default')
415
409 def test_traits(self):
416 def test_traits(self):
410 class A(HasTraits):
417 class A(HasTraits):
411 i = Int
418 i = Int
@@ -458,8 +458,8 class TraitType(object):
458 % (self.name, self.info(), repr_type(value))
458 % (self.name, self.info(), repr_type(value))
459 raise TraitError(e)
459 raise TraitError(e)
460
460
461 def get_metadata(self, key):
461 def get_metadata(self, key, default=None):
462 return getattr(self, '_metadata', {}).get(key, None)
462 return getattr(self, '_metadata', {}).get(key, default)
463
463
464 def set_metadata(self, key, value):
464 def set_metadata(self, key, value):
465 getattr(self, '_metadata', {})[key] = value
465 getattr(self, '_metadata', {})[key] = value
@@ -728,7 +728,7 class HasTraits(py3compat.with_metaclass(MetaHasTraits, object)):
728
728
729 return result
729 return result
730
730
731 def trait_metadata(self, traitname, key):
731 def trait_metadata(self, traitname, key, default=None):
732 """Get metadata values for trait by key."""
732 """Get metadata values for trait by key."""
733 try:
733 try:
734 trait = getattr(self.__class__, traitname)
734 trait = getattr(self.__class__, traitname)
@@ -736,7 +736,7 class HasTraits(py3compat.with_metaclass(MetaHasTraits, object)):
736 raise TraitError("Class %s does not have a trait named %s" %
736 raise TraitError("Class %s does not have a trait named %s" %
737 (self.__class__.__name__, traitname))
737 (self.__class__.__name__, traitname))
738 else:
738 else:
739 return trait.get_metadata(key)
739 return trait.get_metadata(key, default)
740
740
741 #-----------------------------------------------------------------------------
741 #-----------------------------------------------------------------------------
742 # Actual TraitTypes implementations/subclasses
742 # Actual TraitTypes implementations/subclasses
General Comments 0
You need to be logged in to leave comments. Login now