diff options
author | Ethan Onstott <storm@stormprograms.com> | 2020-04-28 17:20:55 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-28 17:20:55 (GMT) |
commit | d9a43e20facdf4ad10186f820601c6580e1baa80 (patch) | |
tree | ff8c2a396025dfa3c37d97b3fe4d3ff516bf9cf4 /Lib/enum.py | |
parent | a42ca74fa30227e2f89a619332557cf093a937d5 (diff) | |
download | cpython-d9a43e20facdf4ad10186f820601c6580e1baa80.zip cpython-d9a43e20facdf4ad10186f820601c6580e1baa80.tar.gz cpython-d9a43e20facdf4ad10186f820601c6580e1baa80.tar.bz2 |
bpo-40025: Require _generate_next_value_ to be defined before members (GH-19098)
require `_generate_next_value_` to be defined before members
Diffstat (limited to 'Lib/enum.py')
-rw-r--r-- | Lib/enum.py | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/Lib/enum.py b/Lib/enum.py index 0be1b60..49b552b 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -60,6 +60,7 @@ class _EnumDict(dict): self._member_names = [] self._last_values = [] self._ignore = [] + self._auto_called = False def __setitem__(self, key, value): """Changes anything not dundered or not a descriptor. @@ -77,6 +78,9 @@ class _EnumDict(dict): ): raise ValueError('_names_ are reserved for future Enum use') if key == '_generate_next_value_': + # check if members already defined as auto() + if self._auto_called: + raise TypeError("_generate_next_value_ must be defined before members") setattr(self, '_generate_next_value', value) elif key == '_ignore_': if isinstance(value, str): @@ -100,6 +104,7 @@ class _EnumDict(dict): # enum overwriting a descriptor? raise TypeError('%r already defined as: %r' % (key, self[key])) if isinstance(value, auto): + self._auto_called = True if value.value == _auto_null: value.value = self._generate_next_value(key, 1, len(self._member_names), self._last_values[:]) value = value.value |