You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Slightly adapting the UseEnum example to use a str based enum makes it fail to recognize the enum by the value:
import enum
from traitlets import HasTraits, UseEnum
class Color(str, enum.Enum):
RED = "red"
BLUE = "blue"
GREEN = "green"
class MyEntity(HasTraits):
color = UseEnum(Color, default_value=Color.BLUE)
entity = MyEntity(color=Color.RED)
entity.color = Color.GREEN
entity.color = "GREEN"
entity.color = "Color.GREEN"
entity.color = "green"
assert entity.color is Color.GREEN
Raises:
❯ python test_enum.py
Traceback (most recent call last):
File "/home/mnoethe/Projects/test_enum.py", line 16, in <module>
entity.color = "green" # USE: number (as int)
File "/home/mnoethe/.local/conda/envs/cta-dev/lib/python3.9/site-packages/traitlets/traitlets.py", line 729, in __set__
self.set(obj, value)
File "/home/mnoethe/.local/conda/envs/cta-dev/lib/python3.9/site-packages/traitlets/traitlets.py", line 703, in set
new_value = self._validate(obj, value)
File "/home/mnoethe/.local/conda/envs/cta-dev/lib/python3.9/site-packages/traitlets/traitlets.py", line 735, in _validate
value = self.validate(obj, value)
File "/home/mnoethe/.local/conda/envs/cta-dev/lib/python3.9/site-packages/traitlets/traitlets.py", line 3582, in validate
self.error(obj, value)
File "/home/mnoethe/.local/conda/envs/cta-dev/lib/python3.9/site-packages/traitlets/traitlets.py", line 841, in error
raise TraitError(e)
traitlets.traitlets.TraitError: The 'color' trait of a MyEntity instance expected any of ['RED', 'BLUE', 'GREEN'], not the str 'green'.
The text was updated successfully, but these errors were encountered:
Slightly adapting the
UseEnum
example to use astr
based enum makes it fail to recognize the enum by the value:Raises:
The text was updated successfully, but these errors were encountered: