from traits.api import HasTraits, Str, Tuple
class Data(HasTraits):
values = Tuple(Str, ...)
data = Data(values=("a", "b", "c"))
Output:
TraitError: The 'values' trait of a Data instance must be a tuple of the form: (a string, an ellipsis or None), but a value of ('a', 'b', 'c') <class 'tuple'> was specified.
I am learning traits api and read the docs, I didn't a find way to pass any specific type to the variable-length tuple in traits. I am getting error! How do i solve this?
Changed values = Tuple(Str, ...) to values = Tuple(Str), Still got:
TraitError: The 'values' trait of a Data instance must be a tuple of the form: (a string), but a value of ('a', 'b', 'c') <class 'tuple'> was specified.
According to the Tuple docs, variable length tuples are not an option:
The use case you're describing, a variable-length sequence of a single type is what you get with a
List(Str), although you do not get immutability. You could fake it, by creating a Property that gets and sets tuples but stores a List under the cover:This produces the output below. It's not quite right because the error message is triggered by the List validator, not the Tuple's.
You could validate the Tuple type with something like the following, but this starts feeling a little icky:
The generated output is: