-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Python Enums as values for Enum trait #685
Conversation
Codecov Report
@@ Coverage Diff @@
## master #685 +/- ##
==========================================
+ Coverage 71.39% 72.35% +0.96%
==========================================
Files 45 51 +6
Lines 6323 6352 +29
Branches 1286 1274 -12
==========================================
+ Hits 4514 4596 +82
+ Misses 1403 1361 -42
+ Partials 406 395 -11
Continue to review full report at Codecov.
|
The TraitsUI piece isn't working correctly right now. |
The issue with the editor is that https://github.com/enthought/traitsui/blob/master/traitsui/editor_factory.py#L317 should be listening to |
value = None | ||
if len(values) > 0: | ||
value = values[0] | ||
value = collection_default(values) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think #389 can be closed by this PR (as of now) without more being done to it.
While #389 concerns the dictionary only, the idea is to relax the requirement on the values
supporting __index__
. As long as values
implements __iter__
, Enum can obtain the allowed values.
I understand your concern with the potential non-deterministic behaviour, e.g. for set
and the dict
prior to Python 3.6. But if, say, values
is an instance of OrderedDict
, the Enum
on master will fail trying to use the first key as a default. One has to make another property traits to return a list from the keys of OrderedDict
. I guess it is about how opinionated this Enum
should be when it comes to setting a default value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To verify:
from traits.api import HasTraits, Enum, Any
class Foo(HasTraits):
a = Enum(values="b")
b = Any()
def _b_default(self):
return {"a": 1, "b": 2}
if __name__ == "__main__":
f = Foo()
# f.configure_traits()
print(repr(f.a))
On this branch, a value 'a'
was printed.
On master, this results in an error:
Traceback (most recent call last):
File "dict_enum.py", line 17, in <module>
print(repr(f.a))
File "/Users/kchoi/ETS/traits/traits/trait_types.py", line 2030, in _get
value = values[0]
KeyError: 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. I think I liked the error better. :-)
I get an error running the test suite under Python 3.8:
|
Related: https://bugs.python.org/issue33217. I'm not sure why it was considered a good idea to raise |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM on a first pass.
I'm not convinced that supporting set
is a good idea, but if we want to do that then there should be tests for that case, and we need to figure out what to do about getting a default value from a set: introducing non-deterministic behaviour (taking the "first" item of the set as the default) seems like a recipe for late-discovered bugs in Traits-using projects.
We need to figure out how to get the containment check to work properly on Python 3.8.
I don't have an easy way to check on Python 3.8, but I have attempted to solve the |
Confirmed working on 3.8. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
A fairly straightforward change. Given:
you can do
Using Python
Enum
with traitsEnum
s can replace the many of the needs for the use of mappedTrait
s since the enums hold both a value and a name out of the box.Edit:
More work on this, with a couple of drive-by fixes:
EditorWithList
classEnum
trait as well.xgetattr
instead ofeval
for dynamic values