diff options
author | Ethan Furman <ethan@stoneleaf.us> | 2018-01-22 07:56:37 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-22 07:56:37 -0800 |
commit | a4b1bb4801f7a941ff9e86b96da539be1c288833 (patch) | |
tree | a5cd81d18b3d10dc8f8b6cdf7d51e8172d5f7ff9 /Lib/enum.py | |
parent | urllib.request: Remove unused import (GH-5268) (diff) | |
download | cpython-a4b1bb4801f7a941ff9e86b96da539be1c288833.tar.gz cpython-a4b1bb4801f7a941ff9e86b96da539be1c288833.tar.bz2 cpython-a4b1bb4801f7a941ff9e86b96da539be1c288833.zip |
bpo-31801: Enum: add _ignore_ as class option (#5237)
* bpo-31801: Enum: add _ignore_ as class option
_ignore_ is a list, or white-space seperated str, of names that will not
be candidates for members; these names, and _ignore_ itself, are removed
from the final class.
* bpo-31801: Enum: add documentation for _ignore_
* bpo-31801: Enum: remove trailing whitespace
* bpo-31801: Enum: fix bulleted list format
* bpo-31801: add version added for _ignore_
Diffstat (limited to 'Lib/enum.py')
-rw-r--r-- | Lib/enum.py | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/Lib/enum.py b/Lib/enum.py index fe7cb20fc06..e5fe6f3b94a 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -64,6 +64,7 @@ class _EnumDict(dict): super().__init__() self._member_names = [] self._last_values = [] + self._ignore = [] def __setitem__(self, key, value): """Changes anything not dundered or not a descriptor. @@ -77,17 +78,28 @@ class _EnumDict(dict): if _is_sunder(key): if key not in ( '_order_', '_create_pseudo_member_', - '_generate_next_value_', '_missing_', + '_generate_next_value_', '_missing_', '_ignore_', ): raise ValueError('_names_ are reserved for future Enum use') if key == '_generate_next_value_': setattr(self, '_generate_next_value', value) + elif key == '_ignore_': + if isinstance(value, str): + value = value.replace(',',' ').split() + else: + value = list(value) + self._ignore = value + already = set(value) & set(self._member_names) + if already: + raise ValueError('_ignore_ cannot specify already set names: %r' % (already, )) elif _is_dunder(key): if key == '__order__': key = '_order_' elif key in self._member_names: # descriptor overwriting an enum? raise TypeError('Attempted to reuse key: %r' % key) + elif key in self._ignore: + pass elif not _is_descriptor(value): if key in self: # enum overwriting a descriptor? @@ -124,6 +136,12 @@ class EnumMeta(type): # cannot be mixed with other types (int, float, etc.) if it has an # inherited __new__ unless a new __new__ is defined (or the resulting # class will fail). + # + # remove any keys listed in _ignore_ + classdict.setdefault('_ignore_', []).append('_ignore_') + ignore = classdict['_ignore_'] + for key in ignore: + classdict.pop(key, None) member_type, first_enum = metacls._get_mixins_(bases) __new__, save_new, use_args = metacls._find_new_(classdict, member_type, first_enum) |