Open mdickinson opened 3 years ago
On next steps: I consider the validation of those dynamic defaults to be a Good Thing, but if this change adversely affects more than a tiny handful of Traits-using projects then we should consider reverting and making a bugfix release.
One other thing: if we decide to keep the behaviour change, it would make sense to add some tests for the validation of the dynamic default.
Given that in 6.1.1 the following raised an error:
from traits.api import Enum, HasStrictTraits, List
class HasDynamicEnumTrait(HasStrictTraits):
selected_language = Enum("JavaScript", "PHP")
def _selected_language_default(self):
return "Python"
print(HasDynamicEnumTrait().selected_language)
I'm generally OK with the behaviour above.
However, this is more than a bit weird:
from traits.api import Enum, HasStrictTraits, List
class HasDynamicEnumTrait(HasStrictTraits):
selected_language = Enum(values='allowed_languages')
allowed_languages = List()
def _selected_language_default(self):
return "Python"
def _allowed_languages_default(self):
return ["JavaScript", "PHP", "Python"]
x = HasDynamicEnumTrait()
print(x.selected_language)
x.allowed_languages = ["JavaScript", "PHP"]
print(x.selected_language)
y = HasDynamicEnumTrait()
y.allowed_languages = ["JavaScript", "PHP"]
print(y.selected_language)
Which gives:
Python
JavaScript
Traceback (most recent call last):
File "/Users/cwebster/src/scratch/enum_defaults.py", line 23, in <module>
print(y.selected_language)
File "/Users/cwebster/.edm/envs/edm/lib/python3.6/site-packages/traits/trait_types.py", line 2101, in _get
value = self.get_value(object, name, trait)
File "/Users/cwebster/.edm/envs/edm/lib/python3.6/site-packages/traits/trait_type.py", line 324, in get_value
object, name
File "/Users/cwebster/.edm/envs/edm/lib/python3.6/site-packages/traits/trait_types.py", line 2119, in _validate
self.error(object, name, value)
File "/Users/cwebster/.edm/envs/edm/lib/python3.6/site-packages/traits/base_trait_handler.py", line 75, in error
object, name, self.full_info(object, name, value), value
traits.trait_errors.TraitError: The 'selected_language' trait of a HasDynamicEnumTrait instance must be 'JavaScript' or 'PHP', but a value of 'Python' <class 'str'> was specified.
Perhaps based on this an invalid default should instead return the first element of the enum?
Argh. Yes, this definitely needs a rethink. The law of unintended consequences strikes again.
PR #1388 fixed validation for dynamic
Enum
s inside a container (e.g., aList
orTuple
).An unanticipated side-effect of that change is that dynamic defaults for dynamic
Enum
s are also now validated, where previously they weren't. (The same will be true for dynamicRange
traits.) This broke some code in a Traits-using project.Given the following code:
Running this code under Traits 6.1.1 gives:
while running the code under Traits 6.2.0 gives: