-
Notifications
You must be signed in to change notification settings - Fork 90
Allow Collections in valid values for Enum trait #889
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
Merged
mdickinson
merged 19 commits into
master
from
feature/allow_collections_for_enum_valid_values
Mar 27, 2020
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
1c00a88
Allow Collections in valid values for Enum trait
fefe0a0
Handle strings correctly
4aab044
Fix import error for python 3.5
adc5c70
Fix test that uses Collection to work on Python3.5
b118044
Handle single enum string as a whole item
3bad7e4
Merge branch 'master' into feature/allow_collections_for_enum_valid_v…
96729b6
Some test changes
0580dbd
PR review changes
fd47a4e
Merge branch 'master' into feature/allow_collections_for_enum_valid_v…
c74c1bf
PR review changes
bfd8f3d
Some refactoring
bdfa427
Cleanup test code
6b3852b
Fix issue #934
0c31e27
Minor change
31546dc
Some refactoring
e5c1412
Some refactoring
3c476c6
Merge branch 'feature/allow_collections_for_enum_valid_values' of git…
ada61f7
Add a comment
78fa1a2
Some more PR changes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,7 +31,6 @@ | |
| get_module_name, | ||
| HandleWeakRef, | ||
| class_of, | ||
| enum_default, | ||
| EnumTypes, | ||
| RangeTypes, | ||
| safe_contains, | ||
|
|
@@ -40,6 +39,7 @@ | |
| Undefined, | ||
| TraitsCache, | ||
| xgetattr, | ||
| is_collection, | ||
| ) | ||
| from .trait_converters import trait_from, trait_cast | ||
| from .trait_dict_object import TraitDictEvent, TraitDictObject | ||
|
|
@@ -1913,16 +1913,24 @@ class BaseEnum(TraitType): | |
| The enumeration of all legal values for the trait. The expected | ||
| signatures are either: | ||
|
|
||
| - a single list, enum.Enum or tuple. The default value is the first | ||
| item in the collection. | ||
| - a collection. The default value is the first item in the | ||
| collection. The collection should conform to the | ||
| collections.abc.Collection interface. That is, it at least | ||
| provides the __contains__, __len__ and __iter__ methods. | ||
| Note that although the types str, bytes, and bytearray | ||
| conform to the collection interface, these are handled | ||
| as discrete units. | ||
| - a single default value, combined with the values keyword | ||
| argument. | ||
| - a default value, followed by a single list enum.Enum or tuple. | ||
| - a default value, followed by a single list enum.Enum, tuple or | ||
| collection conforming to collections.abc.Collection | ||
|
midhun-pm marked this conversation as resolved.
|
||
| - arbitrary positional arguments each giving a valid value. | ||
| values : str | ||
| The name of a trait holding the legal values. A default value may | ||
| be provided via a positional argument, otherwise it is the first | ||
| item stored in the . | ||
| be provided via a positional argument, otherwise the first item in | ||
| the collection is used as the default value. Note that if the | ||
| collection does not have a notion of order like a set, the default | ||
| value will be an arbitrary element from the set. | ||
| **metadata | ||
| Trait metadata for the trait. | ||
|
|
||
|
|
@@ -1953,19 +1961,51 @@ def __init__(self, *args, **metadata): | |
| "when using the 'values' keyword" | ||
| ) | ||
| else: | ||
| default_value = args[0] | ||
| if (len(args) == 1) and isinstance(default_value, EnumTypes): | ||
| args = default_value | ||
| default_value = enum_default(args) | ||
| elif (len(args) == 2) and isinstance(args[1], EnumTypes): | ||
| args = args[1] | ||
| if len(args) < 1: | ||
| raise TraitError("Enum trait requires at " | ||
| "least 1 argument.") | ||
|
|
||
| elif len(args) == 1: | ||
| arg = args[0] | ||
| if isinstance(arg, EnumTypes): | ||
| default_value = next(iter(arg), None) | ||
| self.values = tuple(arg) | ||
|
|
||
| # Treat str, bytes and bytearray as discrete units, | ||
| # and not as a collection. | ||
| elif isinstance(arg, (str, bytes, bytearray)): | ||
|
midhun-pm marked this conversation as resolved.
|
||
| default_value = arg | ||
| self.values = (arg,) | ||
|
|
||
| # Handle a collection | ||
| else: | ||
| default_value = next(iter(arg), None) | ||
| self.values = arg | ||
|
|
||
| elif len(args) == 2: | ||
| # If 2 args, the first is default, second is allowed values. | ||
| default_value = args[0] | ||
| allowed_vals = args[1] | ||
|
|
||
| # Treat str, bytes and bytearray as discrete units, | ||
| # and not as a collection. | ||
| if isinstance(allowed_vals, (str, bytes, bytearray)): | ||
| self.values = tuple(args) | ||
|
|
||
| elif is_collection(allowed_vals): | ||
| self.values = tuple(allowed_vals) | ||
|
|
||
| else: | ||
| self.values = tuple(args) | ||
|
midhun-pm marked this conversation as resolved.
|
||
| else: | ||
| default_value = args[0] | ||
| self.values = tuple(args) | ||
|
|
||
| if isinstance(args, enum.EnumMeta): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @corranwebster Do you know what this branch is for? As far as I can tell, |
||
| metadata.setdefault('format_func', operator.attrgetter('name')) | ||
| metadata.setdefault('evaluate', args) | ||
|
|
||
| self.name = "" | ||
| self.values = tuple(args) | ||
| self.init_fast_validate(ValidateTrait.enum, self.values) | ||
|
|
||
| super(BaseEnum, self).__init__(default_value, **metadata) | ||
|
|
@@ -2019,7 +2059,7 @@ def _get(self, object, name, trait): | |
| value = self.get_value(object, name, trait) | ||
| values = xgetattr(object, self.name) | ||
| if not safe_contains(value, values): | ||
| value = enum_default(values) | ||
| value = next(iter(values), None) | ||
| return value | ||
|
|
||
| def _set(self, object, name, value): | ||
|
|
@@ -2044,17 +2084,24 @@ class Enum(BaseEnum): | |
| The enumeration of all legal values for the trait. The expected | ||
| signatures are either: | ||
|
|
||
| - a single list, enum.Enum or tuple. The default value is the first | ||
| item in the collection. | ||
| - a single list, enum.Enum, tuple or a collection. The default value | ||
| is the first item in the collection. The collection should conform to | ||
| the collections.abc.Collection interface. That is, it at least | ||
| provides the __contains__, __len__ and __iter__ methods. | ||
| Note that although the types str, bytes, and bytearray | ||
| conform to the collection interface, these are handled | ||
| as discrete units. | ||
| - a single default value, combined with the values keyword | ||
| argument. | ||
| - a default value, followed by a single list enum.Enum or tuple. | ||
| - a default value, followed by a single list enum.Enum, tuple or | ||
| collection conforming to collections.abc.Collection | ||
| - arbitrary positional arguments each giving a valid value. | ||
|
|
||
| values : str | ||
| The name of a trait holding the legal values. A default value may | ||
| be provided via a positional argument, otherwise it is the first | ||
| item stored in the . | ||
| be provided via a positional argument, otherwise the first item in | ||
| the collection is used as the default value. Note that if the | ||
| collection does not have a notion of order like a set, the default | ||
| value will be an arbitrary element from the set. | ||
| **metadata | ||
| Trait metadata for the trait. | ||
|
|
||
|
|
@@ -2070,6 +2117,10 @@ class Enum(BaseEnum): | |
|
|
||
| def init_fast_validate(self, *args): | ||
| """ Set up C-level fast validation. """ | ||
| # Don't use fast validation if second arg is not a tuple. | ||
| if len(args) == 2 and not isinstance(args[1], tuple): | ||
| return | ||
|
|
||
| self.fast_validate = args | ||
|
|
||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.