Show More
@@ -809,8 +809,12 b' class Instance(ClassBasedTraitType):' | |||||
809 | """A trait whose value must be an instance of a specified class. |
|
809 | """A trait whose value must be an instance of a specified class. | |
810 |
|
810 | |||
811 | The value can also be an instance of a subclass of the specified class. |
|
811 | The value can also be an instance of a subclass of the specified class. | |
|
812 | ||||
|
813 | Subclasses can declare default classes by overriding the klass attribute | |||
812 | """ |
|
814 | """ | |
813 |
|
815 | |||
|
816 | klass = None | |||
|
817 | ||||
814 | def __init__(self, klass=None, args=None, kw=None, |
|
818 | def __init__(self, klass=None, args=None, kw=None, | |
815 | allow_none=True, **metadata ): |
|
819 | allow_none=True, **metadata ): | |
816 | """Construct an Instance trait. |
|
820 | """Construct an Instance trait. | |
@@ -836,14 +840,17 b' class Instance(ClassBasedTraitType):' | |||||
836 | ----- |
|
840 | ----- | |
837 | If both ``args`` and ``kw`` are None, then the default value is None. |
|
841 | If both ``args`` and ``kw`` are None, then the default value is None. | |
838 | If ``args`` is a tuple and ``kw`` is a dict, then the default is |
|
842 | If ``args`` is a tuple and ``kw`` is a dict, then the default is | |
839 |
created as ``klass(*args, **kw)``. If e |
|
843 | created as ``klass(*args, **kw)``. If exactly one of ``args`` or ``kw`` is | |
840 |
|
|
844 | None, the None is replaced by ``()`` or ``{}``, respectively. | |
841 | """ |
|
845 | """ | |
842 |
|
846 | if klass is None: | ||
843 | if (klass is None) or (not (inspect.isclass(klass) or isinstance(klass, py3compat.string_types))): |
|
847 | klass = self.klass | |
844 | raise TraitError('The klass argument must be a class' |
|
848 | ||
845 | ' you gave: %r' % klass) |
|
849 | if (klass is not None) and (inspect.isclass(klass) or isinstance(klass, py3compat.string_types)): | |
846 | self.klass = klass |
|
850 | self.klass = klass | |
|
851 | else: | |||
|
852 | raise TraitError('The klass attribute must be a class' | |||
|
853 | ' not: %r' % klass) | |||
847 |
|
854 | |||
848 | # self.klass is a class, so handle default_value |
|
855 | # self.klass is a class, so handle default_value | |
849 | if args is None and kw is None: |
|
856 | if args is None and kw is None: |
General Comments 0
You need to be logged in to leave comments.
Login now