In some applications, I've found that Enthought Traits.api is a helpful addition to support static variable types in python.
I'm trying to use the TraitList() item_validator keyword, but using the item_validator keyword threw an error... I tried this...
from traits.api import HasTraits, HasRequiredTraits, TraitList, TraitError
from traits.api import Regex, Enum
def garage_item_validator(item):
"""Validate item adjectives and reject pink or floral items"""
try:
if isinstance(item, Tool):
if item.adjective!="pink" or item.adjective!="floral":
return item
else:
raise ValueError()
except ValueError():
raise TraitError(f"Cannot put {item} in the Garage()")
class Tool(HasRequiredTraits):
name = Regex(regex=r"[Ww]rench|[Ll]awnmower", required=True)
adjective = Enum(*["brown", "rusty", "pink", "floral"], required=True)
def __init__(self, name, adjective):
self.name = name
self.adjective = adjective
def __repr__(self):
return """<Tool: {}>""".format(self.name)
class Garage(HasTraits):
things = TraitList(Tool, item_validator=garage_item_validator) # <---- TraitList() doesn't work
def __init__(self):
self.things = list()
if __name__=="__main__":
my_garage = Garage()
my_garage.things.append(Tool("Lawnmower", "brown"))
my_garage.things.append(Tool("wrench", "pink"))
print(my_garage)
This throws: TypeError: __init__() got an unexpected keyword argument 'item_validator' although the TraitList docs clearly say item_validator is supported.
I also tried to use a traits.api List(), but it just silently ignores the item_validator keyword.
What should I use to validate the contents of a traits list?
TraitListisn't aTrait, but rather a subclass oflistthat performs validation and fires events. It is used internally by theListtrait:and it gets its
item_validatorset by theListtrait:So although there is no hook to change this validator, it does use the validator from the Trait used as the
traitargument theList(Intin the above example, so the list will only hold integer items).So you can achieve your goal by writing a custom trait that validates the way that you want.
In your example you want the items to be instances of
Toolwith certain properties for theadjective, so an Instance trait is a good starting point:which gives an error as desired: