##// END OF EJS Templates
Allow Type trait to be set with an importable string
Thomas Kluyver -
Show More
@@ -531,6 +531,15 b' class TestType(TestCase):'
531
531
532 self.assertRaises(TraitError, setattr, a, 'klass', 10)
532 self.assertRaises(TraitError, setattr, a, 'klass', 10)
533
533
534 def test_set_str_klass(self):
535
536 class A(HasTraits):
537 klass = Type()
538
539 a = A(klass='IPython.utils.ipstruct.Struct')
540 from IPython.utils.ipstruct import Struct
541 self.assertEqual(a.klass, Struct)
542
534 class TestInstance(TestCase):
543 class TestInstance(TestCase):
535
544
536 def test_basic(self):
545 def test_basic(self):
@@ -755,6 +755,12 b' class Type(ClassBasedTraitType):'
755
755
756 def validate(self, obj, value):
756 def validate(self, obj, value):
757 """Validates that the value is a valid object instance."""
757 """Validates that the value is a valid object instance."""
758 if isinstance(value, py3compat.string_types):
759 try:
760 value = import_item(value)
761 except ImportError:
762 raise TraitError("The '%s' trait of %s instance must be a type, but "
763 "%r could not be imported" % (self.name, obj, value))
758 try:
764 try:
759 if issubclass(value, self.klass):
765 if issubclass(value, self.klass):
760 return value
766 return value
General Comments 0
You need to be logged in to leave comments. Login now