traits
traits copied to clipboard
Deprecate acceptance of lists by BaseTuple and Tuple traits
Motivated by #1619
BaseTuple
and Tuple
are somewhat inconsistent about when they accept lists, and it's a bit surprising that they ever accept lists:
-
BaseTuple
always accepts lists - a simple
Tuple()
trait accepts lists, but a trait with explicit item types likeTuple(Int(), Str())
does not
>>> from traits.api import *
>>> class A(HasTraits):
... bt_no_types = BaseTuple()
... bt = BaseTuple(Int, Str)
... t_no_types = Tuple()
... t = Tuple(Int, Str)
...
>>> a = A()
>>> a.bt_no_types = [2, "2"]
>>> a.bt = [2, "2"]
>>> a.t_no_types = [2, "2"]
>>> a.t = [2, "2"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/mdickinson/Enthought/ETS/traits/traits/base_trait_handler.py", line 74, in error
raise TraitError(
traits.trait_errors.TraitError: The 't' trait of an A instance must be a tuple of the form: (an integer, a string), but a value of [2, '2'] <class 'list'> was specified.
I propose getting rid of the inconsistency and only ever accepting tuples for both Tuple
and BaseTuple
traits. This would be a backwards incompatible change, so we'd at least need a deprecation warning period.