diff options
-rw-r--r-- | Doc/library/enum.rst | 4 | ||||
-rw-r--r-- | Lib/enum.py | 5 | ||||
-rw-r--r-- | Lib/test/test_enum.py | 10 | ||||
-rw-r--r-- | Misc/ACKS | 1 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2020-03-21-05-26-38.bpo-40025.DTLtyq.rst | 1 |
5 files changed, 21 insertions, 0 deletions
diff --git a/Doc/library/enum.rst b/Doc/library/enum.rst index 7538084767d..2649b7d8a51 100644 --- a/Doc/library/enum.rst +++ b/Doc/library/enum.rst @@ -273,6 +273,10 @@ overridden:: the next :class:`int` in sequence with the last :class:`int` provided, but the way it does this is an implementation detail and may change. +.. note:: + + The :meth:`_generate_next_value_` method must be defined before any members. + Iteration --------- diff --git a/Lib/enum.py b/Lib/enum.py index 108d389d948..14cc00e7839 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 diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index 47081cf75ca..a2a3c567349 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -1702,6 +1702,16 @@ class TestEnum(unittest.TestCase): self.assertEqual(Color.blue.value, 2) self.assertEqual(Color.green.value, 3) + def test_auto_order(self): + with self.assertRaises(TypeError): + class Color(Enum): + red = auto() + green = auto() + blue = auto() + def _generate_next_value_(name, start, count, last): + return name + + def test_duplicate_auto(self): class Dupes(Enum): first = primero = auto() diff --git a/Misc/ACKS b/Misc/ACKS index 34a6fc439e8..e85e370c98c 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1217,6 +1217,7 @@ Adam Olsen Bryan Olson Grant Olson Koray Oner +Ethan Onstott Piet van Oostrum Tomas Oppelstrup Jason Orendorff diff --git a/Misc/NEWS.d/next/Library/2020-03-21-05-26-38.bpo-40025.DTLtyq.rst b/Misc/NEWS.d/next/Library/2020-03-21-05-26-38.bpo-40025.DTLtyq.rst new file mode 100644 index 00000000000..7b699de4e07 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-03-21-05-26-38.bpo-40025.DTLtyq.rst @@ -0,0 +1 @@ +Raise TypeError when _generate_next_value_ is defined after members. Patch by Ethan Onstott.
\ No newline at end of file |