##// END OF EJS Templates
Fix Tuple behavior wrt allow_none
Sylvain Corlay -
Show More
@@ -1010,15 +1010,15 b' class TestLenList(TraitTestBase):'
1010
1010
1011 class TupleTrait(HasTraits):
1011 class TupleTrait(HasTraits):
1012
1012
1013 value = Tuple(Int(allow_none=True))
1013 value = Tuple(Int(allow_none=True), default_value=(1,))
1014
1014
1015 class TestTupleTrait(TraitTestBase):
1015 class TestTupleTrait(TraitTestBase):
1016
1016
1017 obj = TupleTrait()
1017 obj = TupleTrait()
1018
1018
1019 _default_value = None
1019 _default_value = (1,)
1020 _good_values = [(1,), None, (0,), [1], (None,)]
1020 _good_values = [(1,), (0,), [1]]
1021 _bad_values = [10, (1,2), ('a'), ()]
1021 _bad_values = [10, (1, 2), ('a'), (), None]
1022
1022
1023 def coerce(self, value):
1023 def coerce(self, value):
1024 if value is not None:
1024 if value is not None:
@@ -1039,8 +1039,8 b' class TestLooseTupleTrait(TraitTestBase):'
1039 obj = LooseTupleTrait()
1039 obj = LooseTupleTrait()
1040
1040
1041 _default_value = (1,2,3)
1041 _default_value = (1,2,3)
1042 _good_values = [(1,), None, [1], (0,), tuple(range(5)), tuple('hello'), ('a',5), ()]
1042 _good_values = [(1,), [1], (0,), tuple(range(5)), tuple('hello'), ('a',5), ()]
1043 _bad_values = [10, 'hello', {}]
1043 _bad_values = [10, 'hello', {}, None]
1044
1044
1045 def coerce(self, value):
1045 def coerce(self, value):
1046 if value is not None:
1046 if value is not None:
@@ -886,8 +886,7 b' class ClassBasedTraitType(TraitType):'
886 class Type(ClassBasedTraitType):
886 class Type(ClassBasedTraitType):
887 """A trait whose value must be a subclass of a specified class."""
887 """A trait whose value must be a subclass of a specified class."""
888
888
889 def __init__ (self, default_value=None, klass=None, allow_none=False,
889 def __init__ (self, default_value=None, klass=None, **metadata):
890 **metadata):
891 """Construct a Type trait
890 """Construct a Type trait
892
891
893 A Type trait specifies that its values must be subclasses of
892 A Type trait specifies that its values must be subclasses of
@@ -923,7 +922,7 b' class Type(ClassBasedTraitType):'
923
922
924 self.klass = klass
923 self.klass = klass
925
924
926 super(Type, self).__init__(default_value, allow_none=allow_none, **metadata)
925 super(Type, self).__init__(default_value, **metadata)
927
926
928 def validate(self, obj, value):
927 def validate(self, obj, value):
929 """Validates that the value is a valid object instance."""
928 """Validates that the value is a valid object instance."""
@@ -987,8 +986,7 b' class Instance(ClassBasedTraitType):'
987
986
988 klass = None
987 klass = None
989
988
990 def __init__(self, klass=None, args=None, kw=None, allow_none=False,
989 def __init__(self, klass=None, args=None, kw=None, **metadata):
991 **metadata ):
992 """Construct an Instance trait.
990 """Construct an Instance trait.
993
991
994 This trait allows values that are instances of a particular
992 This trait allows values that are instances of a particular
@@ -1042,7 +1040,7 b' class Instance(ClassBasedTraitType):'
1042
1040
1043 default_value = DefaultValueGenerator(*args, **kw)
1041 default_value = DefaultValueGenerator(*args, **kw)
1044
1042
1045 super(Instance, self).__init__(default_value, allow_none=allow_none, **metadata)
1043 super(Instance, self).__init__(default_value, **metadata)
1046
1044
1047 def validate(self, obj, value):
1045 def validate(self, obj, value):
1048 if isinstance(value, self.klass):
1046 if isinstance(value, self.klass):
@@ -1461,8 +1459,7 b' class Container(Instance):'
1461 _valid_defaults = SequenceTypes
1459 _valid_defaults = SequenceTypes
1462 _trait = None
1460 _trait = None
1463
1461
1464 def __init__(self, trait=None, default_value=None, allow_none=False,
1462 def __init__(self, trait=None, default_value=None, **metadata):
1465 **metadata):
1466 """Create a container trait type from a list, set, or tuple.
1463 """Create a container trait type from a list, set, or tuple.
1467
1464
1468 The default value is created by doing ``List(default_value)``,
1465 The default value is created by doing ``List(default_value)``,
@@ -1512,8 +1509,7 b' class Container(Instance):'
1512 elif trait is not None:
1509 elif trait is not None:
1513 raise TypeError("`trait` must be a Trait or None, got %s"%repr_type(trait))
1510 raise TypeError("`trait` must be a Trait or None, got %s"%repr_type(trait))
1514
1511
1515 super(Container,self).__init__(klass=self.klass, args=args,
1512 super(Container,self).__init__(klass=self.klass, args=args, **metadata)
1516 allow_none=allow_none, **metadata)
1517
1513
1518 def element_error(self, obj, element, validator):
1514 def element_error(self, obj, element, validator):
1519 e = "Element of the '%s' trait of %s instance must be %s, but a value of %s was specified." \
1515 e = "Element of the '%s' trait of %s instance must be %s, but a value of %s was specified." \
@@ -1668,7 +1664,6 b' class Tuple(Container):'
1668
1664
1669 """
1665 """
1670 default_value = metadata.pop('default_value', None)
1666 default_value = metadata.pop('default_value', None)
1671 allow_none = metadata.pop('allow_none', True)
1672
1667
1673 # allow Tuple((values,)):
1668 # allow Tuple((values,)):
1674 if len(traits) == 1 and default_value is None and not is_trait(traits[0]):
1669 if len(traits) == 1 and default_value is None and not is_trait(traits[0]):
@@ -1691,7 +1686,7 b' class Tuple(Container):'
1691 if self._traits and default_value is None:
1686 if self._traits and default_value is None:
1692 # don't allow default to be an empty container if length is specified
1687 # don't allow default to be an empty container if length is specified
1693 args = None
1688 args = None
1694 super(Container,self).__init__(klass=self.klass, args=args, allow_none=allow_none, **metadata)
1689 super(Container,self).__init__(klass=self.klass, args=args, **metadata)
1695
1690
1696 def validate_elements(self, obj, value):
1691 def validate_elements(self, obj, value):
1697 if not self._traits:
1692 if not self._traits:
@@ -1724,7 +1719,7 b' class Dict(Instance):'
1724 """An instance of a Python dict."""
1719 """An instance of a Python dict."""
1725 _trait = None
1720 _trait = None
1726
1721
1727 def __init__(self, trait=None, default_value=NoDefaultSpecified, allow_none=False, **metadata):
1722 def __init__(self, trait=None, default_value=NoDefaultSpecified, **metadata):
1728 """Create a dict trait type from a dict.
1723 """Create a dict trait type from a dict.
1729
1724
1730 The default value is created by doing ``dict(default_value)``,
1725 The default value is created by doing ``dict(default_value)``,
@@ -1764,8 +1759,7 b' class Dict(Instance):'
1764 elif trait is not None:
1759 elif trait is not None:
1765 raise TypeError("`trait` must be a Trait or None, got %s"%repr_type(trait))
1760 raise TypeError("`trait` must be a Trait or None, got %s"%repr_type(trait))
1766
1761
1767 super(Dict,self).__init__(klass=dict, args=args,
1762 super(Dict,self).__init__(klass=dict, args=args, **metadata)
1768 allow_none=allow_none, **metadata)
1769
1763
1770 def element_error(self, obj, element, validator):
1764 def element_error(self, obj, element, validator):
1771 e = "Element of the '%s' trait of %s instance must be %s, but a value of %s was specified." \
1765 e = "Element of the '%s' trait of %s instance must be %s, but a value of %s was specified." \
@@ -1803,7 +1797,7 b' class Dict(Instance):'
1803 class EventfulDict(Instance):
1797 class EventfulDict(Instance):
1804 """An instance of an EventfulDict."""
1798 """An instance of an EventfulDict."""
1805
1799
1806 def __init__(self, default_value={}, allow_none=False, **metadata):
1800 def __init__(self, default_value={}, **metadata):
1807 """Create a EventfulDict trait type from a dict.
1801 """Create a EventfulDict trait type from a dict.
1808
1802
1809 The default value is created by doing
1803 The default value is created by doing
@@ -1820,13 +1814,13 b' class EventfulDict(Instance):'
1820 raise TypeError('default value of EventfulDict was %s' % default_value)
1814 raise TypeError('default value of EventfulDict was %s' % default_value)
1821
1815
1822 super(EventfulDict, self).__init__(klass=eventful.EventfulDict, args=args,
1816 super(EventfulDict, self).__init__(klass=eventful.EventfulDict, args=args,
1823 allow_none=allow_none, **metadata)
1817 **metadata)
1824
1818
1825
1819
1826 class EventfulList(Instance):
1820 class EventfulList(Instance):
1827 """An instance of an EventfulList."""
1821 """An instance of an EventfulList."""
1828
1822
1829 def __init__(self, default_value=None, allow_none=False, **metadata):
1823 def __init__(self, default_value=None, **metadata):
1830 """Create a EventfulList trait type from a dict.
1824 """Create a EventfulList trait type from a dict.
1831
1825
1832 The default value is created by doing
1826 The default value is created by doing
@@ -1839,7 +1833,7 b' class EventfulList(Instance):'
1839 args = (default_value,)
1833 args = (default_value,)
1840
1834
1841 super(EventfulList, self).__init__(klass=eventful.EventfulList, args=args,
1835 super(EventfulList, self).__init__(klass=eventful.EventfulList, args=args,
1842 allow_none=allow_none, **metadata)
1836 **metadata)
1843
1837
1844
1838
1845 class TCPAddress(TraitType):
1839 class TCPAddress(TraitType):
General Comments 0
You need to be logged in to leave comments. Login now