From 97a6a5985da5264b3e6979f79384eacfbc57974c 2014-02-25 21:53:05 From: Jonathan Frederic Date: 2014-02-25 21:53:05 Subject: [PATCH] Merge pull request #14 from minrk/cast-tuple Cast from list in Tuple traitlet --- diff --git a/IPython/utils/tests/test_traitlets.py b/IPython/utils/tests/test_traitlets.py index 87f29fb..83fae6e 100644 --- a/IPython/utils/tests/test_traitlets.py +++ b/IPython/utils/tests/test_traitlets.py @@ -886,8 +886,13 @@ class TestList(TraitTestBase): obj = ListTrait() _default_value = [] - _good_values = [[], [1], list(range(10))] - _bad_values = [10, [1,'a'], 'a', (1,2)] + _good_values = [[], [1], list(range(10)), (1,2)] + _bad_values = [10, [1,'a'], 'a'] + + def coerce(self, value): + if value is not None: + value = list(value) + return value class LenListTrait(HasTraits): @@ -898,8 +903,13 @@ class TestLenList(TraitTestBase): obj = LenListTrait() _default_value = [0] - _good_values = [[1], list(range(2))] - _bad_values = [10, [1,'a'], 'a', (1,2), [], list(range(3))] + _good_values = [[1], [1,2], (1,2)] + _bad_values = [10, [1,'a'], 'a', [], list(range(3))] + + def coerce(self, value): + if value is not None: + value = list(value) + return value class TupleTrait(HasTraits): @@ -910,8 +920,13 @@ class TestTupleTrait(TraitTestBase): obj = TupleTrait() _default_value = None - _good_values = [(1,), None,(0,)] - _bad_values = [10, (1,2), [1],('a'), ()] + _good_values = [(1,), None, (0,), [1]] + _bad_values = [10, (1,2), ('a'), ()] + + def coerce(self, value): + if value is not None: + value = tuple(value) + return value def test_invalid_args(self): self.assertRaises(TypeError, Tuple, 5) @@ -927,8 +942,13 @@ class TestLooseTupleTrait(TraitTestBase): obj = LooseTupleTrait() _default_value = (1,2,3) - _good_values = [(1,), None, (0,), tuple(range(5)), tuple('hello'), ('a',5), ()] - _bad_values = [10, 'hello', [1], []] + _good_values = [(1,), None, [1], (0,), tuple(range(5)), tuple('hello'), ('a',5), ()] + _bad_values = [10, 'hello', {}] + + def coerce(self, value): + if value is not None: + value = tuple(value) + return value def test_invalid_args(self): self.assertRaises(TypeError, Tuple, 5) diff --git a/IPython/utils/traitlets.py b/IPython/utils/traitlets.py index 86fb103..ddea7b7 100644 --- a/IPython/utils/traitlets.py +++ b/IPython/utils/traitlets.py @@ -1210,6 +1210,7 @@ class Container(Instance): To be subclassed by overriding klass. """ klass = None + _cast_types = () _valid_defaults = SequenceTypes _trait = None @@ -1273,6 +1274,8 @@ class Container(Instance): raise TraitError(e) def validate(self, obj, value): + if isinstance(value, self._cast_types): + value = self.klass(value) value = super(Container, self).validate(obj, value) if value is None: return value @@ -1298,6 +1301,7 @@ class Container(Instance): class List(Container): """An instance of a Python list.""" klass = list + _cast_types = (tuple,) def __init__(self, trait=None, default_value=None, minlen=0, maxlen=sys.maxsize, allow_none=True, **metadata): @@ -1354,15 +1358,27 @@ class List(Container): self.length_error(obj, value) return super(List, self).validate_elements(obj, value) + + def validate(self, obj, value): + value = super(List, self).validate(obj, value) + if value is None: + return value + + value = self.validate_elements(obj, value) + + return value + -class Set(Container): +class Set(List): """An instance of a Python set.""" klass = set + _cast_types = (tuple, list) class Tuple(Container): """An instance of a Python tuple.""" klass = tuple + _cast_types = (list,) def __init__(self, *traits, **metadata): """Tuple(*traits, default_value=None, allow_none=True, **medatata)