From 3e8480c2401fde4fea9e2f0796a31a076bfd329a Mon Sep 17 00:00:00 2001 From: Oscar R Date: Mon, 11 Apr 2022 16:58:19 -0400 Subject: [PATCH 1/7] Fix issue affecting the use of auto() alongside aliases in Enums --- Lib/enum.py | 2 +- .../next/Library/2022-04-11-16-55-41.gh-issue-91456.DK3KKl.rst | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2022-04-11-16-55-41.gh-issue-91456.DK3KKl.rst diff --git a/Lib/enum.py b/Lib/enum.py index decb601496fc9b..2a035dae2ea00b 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -1214,7 +1214,7 @@ def _generate_next_value_(name, start, count, last_values): count: the number of existing members last_value: the last value assigned or None """ - for last_value in reversed(last_values): + for last_value in sorted(last_values, reverse=True): try: return last_value + 1 except TypeError: diff --git a/Misc/NEWS.d/next/Library/2022-04-11-16-55-41.gh-issue-91456.DK3KKl.rst b/Misc/NEWS.d/next/Library/2022-04-11-16-55-41.gh-issue-91456.DK3KKl.rst new file mode 100644 index 00000000000000..16ab3d4bac0505 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-04-11-16-55-41.gh-issue-91456.DK3KKl.rst @@ -0,0 +1,2 @@ +Fixed an issue with Enums where the default behavior of auto() was +generating incorrect values following alias assignment From 61ac2237263ce7141fc6c8bfe656aa8723bd19ba Mon Sep 17 00:00:00 2001 From: Oscar R Date: Mon, 11 Apr 2022 18:23:57 -0400 Subject: [PATCH 2/7] Improve Fix and add test case as requested --- Lib/enum.py | 3 ++- Lib/test/test_enum.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Lib/enum.py b/Lib/enum.py index 2a035dae2ea00b..726a24a4de616d 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -1214,7 +1214,8 @@ def _generate_next_value_(name, start, count, last_values): count: the number of existing members last_value: the last value assigned or None """ - for last_value in sorted(last_values, reverse=True): + numerical_last_values = [x for x in last_values if isinstance(x, int) or isinstance(x, float)] + for last_value in sorted(numerical_last_values, reverse=True): try: return last_value + 1 except TypeError: diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index e26ef000ea76c3..c8271ff178c232 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -3991,6 +3991,18 @@ def _generate_next_value_(name, start, count, last): self.assertEqual(Color.red.value, 'pathological case') self.assertEqual(Color.blue.value, 'blue') + def test_auto_with_aliases(self): + class Color(Enum): + red = auto() + blue = auto() + oxford = blue + crimson = red + green = auto() + self.assertIs(Color.crimson, Color.red) + self.assertIs(Color.oxford, Color.blue) + self.assertIsNot(Color.green, Color.red) + self.assertIsNot(Color.green, Color.blue) + def test_duplicate_auto(self): class Dupes(Enum): first = primero = auto() From eec39934068f80442fbe89e0e6ac84ebb1e03372 Mon Sep 17 00:00:00 2001 From: Oscar R Date: Thu, 14 Apr 2022 13:16:55 -0400 Subject: [PATCH 3/7] Check Incrementable instead of type checking --- Lib/enum.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Lib/enum.py b/Lib/enum.py index 726a24a4de616d..2f470444a9c0f7 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -1214,12 +1214,15 @@ def _generate_next_value_(name, start, count, last_values): count: the number of existing members last_value: the last value assigned or None """ - numerical_last_values = [x for x in last_values if isinstance(x, int) or isinstance(x, float)] - for last_value in sorted(numerical_last_values, reverse=True): + incrementable_last_values = [] + for val in last_values: try: - return last_value + 1 + incrementable_last_values.append(val + 1) except TypeError: pass + + if incrementable_last_values: + return sorted(incrementable_last_values)[-1] else: return start From 606e29af100f09ce2dc61f44a8bd7e5dbd65cb95 Mon Sep 17 00:00:00 2001 From: Oscar R Date: Thu, 14 Apr 2022 13:25:35 -0400 Subject: [PATCH 4/7] Update function doc_string for last_values arg --- Lib/enum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/enum.py b/Lib/enum.py index 2f470444a9c0f7..165ca0f21447c0 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -1212,7 +1212,7 @@ def _generate_next_value_(name, start, count, last_values): name: the name of the member start: the initial start value or None count: the number of existing members - last_value: the last value assigned or None + last_values: the list of values assigned """ incrementable_last_values = [] for val in last_values: From c3e91e80a246b87e450cf9879c9cffeb27872e0f Mon Sep 17 00:00:00 2001 From: Oscar R Date: Mon, 18 Apr 2022 10:50:46 -0400 Subject: [PATCH 5/7] Improve time complexity by using filter() --- Lib/enum.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/Lib/enum.py b/Lib/enum.py index 165ca0f21447c0..327bb56e009521 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -1214,15 +1214,18 @@ def _generate_next_value_(name, start, count, last_values): count: the number of existing members last_values: the list of values assigned """ - incrementable_last_values = [] - for val in last_values: - try: - incrementable_last_values.append(val + 1) - except TypeError: - pass - if incrementable_last_values: - return sorted(incrementable_last_values)[-1] + # Filter funciton to deal with last_values lists of mixed types + def test_incrementable(n): + try: + n + 1 + return True + except: + return False + + checked_last_values = sorted(filter(test_incrementable, last_values)) + if checked_last_values: + return checked_last_values[-1] + 1 else: return start From 8769a2ee42397f8d239f10c65e2fe3848aa02163 Mon Sep 17 00:00:00 2001 From: Oscar R Date: Mon, 18 Apr 2022 11:48:08 -0400 Subject: [PATCH 6/7] Forgot to specify exception type (TypeError) --- Lib/enum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/enum.py b/Lib/enum.py index 327bb56e009521..5e97ef3c09fee9 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -1220,7 +1220,7 @@ def test_incrementable(n): try: n + 1 return True - except: + except TypeError: return False checked_last_values = sorted(filter(test_incrementable, last_values)) From d65aaf4d3a092d32059dc7d978a2aab685b78cb6 Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Wed, 22 Jun 2022 19:57:41 -0700 Subject: [PATCH 7/7] deprecate current behavior --- Doc/library/enum.rst | 4 ++ Lib/enum.py | 41 ++++++++------ Lib/test/test_enum.py | 53 +++++++++++++++---- ...2-04-11-16-55-41.gh-issue-91456.DK3KKl.rst | 5 +- 4 files changed, 77 insertions(+), 26 deletions(-) diff --git a/Doc/library/enum.rst b/Doc/library/enum.rst index c3256c56c6366f..b1333c7dd5cf9d 100644 --- a/Doc/library/enum.rst +++ b/Doc/library/enum.rst @@ -761,6 +761,10 @@ Utilities and Decorators ``_generate_next_value_`` can be overridden to customize the values used by *auto*. + .. note:: in 3.13 the default ``"generate_next_value_`` will always return + the highest member value incremented by 1, and will fail if any + member is an incompatible type. + .. decorator:: property A decorator similar to the built-in *property*, but specifically for diff --git a/Lib/enum.py b/Lib/enum.py index 5e97ef3c09fee9..8d0982a218d766 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -1205,28 +1205,39 @@ def __new__(cls, value): def __init__(self, *args, **kwds): pass - def _generate_next_value_(name, start, count, last_values): + def _generate_next_value_(name, start, count, last_value): """ Generate the next value when not given. name: the name of the member start: the initial start value or None count: the number of existing members - last_values: the list of values assigned + last_value: the list of values assigned """ - - # Filter funciton to deal with last_values lists of mixed types - def test_incrementable(n): - try: - n + 1 - return True - except TypeError: - return False - - checked_last_values = sorted(filter(test_incrementable, last_values)) - if checked_last_values: - return checked_last_values[-1] + 1 - else: + if not last_value: + return start + try: + last = last_value[-1] + last_value.sort() + if last == last_value[-1]: + # no difference between old and new methods + return last + 1 + else: + # trigger old method (with warning) + raise TypeError + except TypeError: + import warnings + warnings.warn( + "In 3.13 the default `auto()`/`_generate_next_value_` will require all values to be sortable and support adding +1\n" + "and the value returned will be the largest value in the enum incremented by 1", + DeprecationWarning, + stacklevel=3, + ) + for v in last_value: + try: + return v + 1 + except TypeError: + pass return start @classmethod diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index c8271ff178c232..528ec0de47d7ac 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -3953,23 +3953,54 @@ class Color(AutoNameEnum): self.assertEqual(Color.blue.value, 'blue') self.assertEqual(Color.green.value, 'green') - def test_auto_garbage(self): - class Color(Enum): - red = 'red' - blue = auto() + @unittest.skipIf( + python_version >= (3, 13), + 'mixed types with auto() no longer supported', + ) + def test_auto_garbage_ok(self): + with self.assertWarnsRegex(DeprecationWarning, 'will require all values to be sortable'): + class Color(Enum): + red = 'red' + blue = auto() self.assertEqual(Color.blue.value, 1) - def test_auto_garbage_corrected(self): - class Color(Enum): - red = 'red' - blue = 2 - green = auto() + @unittest.skipIf( + python_version >= (3, 13), + 'mixed types with auto() no longer supported', + ) + def test_auto_garbage_corrected_ok(self): + with self.assertWarnsRegex(DeprecationWarning, 'will require all values to be sortable'): + class Color(Enum): + red = 'red' + blue = 2 + green = auto() self.assertEqual(list(Color), [Color.red, Color.blue, Color.green]) self.assertEqual(Color.red.value, 'red') self.assertEqual(Color.blue.value, 2) self.assertEqual(Color.green.value, 3) + @unittest.skipIf( + python_version < (3, 13), + 'mixed types with auto() will raise in 3.13', + ) + def test_auto_garbage_fail(self): + with self.assertRaisesRegex(TypeError, 'will require all values to be sortable'): + class Color(Enum): + red = 'red' + blue = auto() + + @unittest.skipIf( + python_version < (3, 13), + 'mixed types with auto() will raise in 3.13', + ) + def test_auto_garbage_corrected_fail(self): + with self.assertRaisesRegex(TypeError, 'will require all values to be sortable'): + class Color(Enum): + red = 'red' + blue = 2 + green = auto() + def test_auto_order(self): with self.assertRaises(TypeError): class Color(Enum): @@ -3991,6 +4022,10 @@ def _generate_next_value_(name, start, count, last): self.assertEqual(Color.red.value, 'pathological case') self.assertEqual(Color.blue.value, 'blue') + @unittest.skipIf( + python_version < (3, 13), + 'auto() will return highest value + 1 in 3.13', + ) def test_auto_with_aliases(self): class Color(Enum): red = auto() diff --git a/Misc/NEWS.d/next/Library/2022-04-11-16-55-41.gh-issue-91456.DK3KKl.rst b/Misc/NEWS.d/next/Library/2022-04-11-16-55-41.gh-issue-91456.DK3KKl.rst index 16ab3d4bac0505..a4c853149bdf02 100644 --- a/Misc/NEWS.d/next/Library/2022-04-11-16-55-41.gh-issue-91456.DK3KKl.rst +++ b/Misc/NEWS.d/next/Library/2022-04-11-16-55-41.gh-issue-91456.DK3KKl.rst @@ -1,2 +1,3 @@ -Fixed an issue with Enums where the default behavior of auto() was -generating incorrect values following alias assignment +Deprecate current default auto() behavior: In 3.13 the default will be for +for auto() to always return the largest member value incremented by +1, and to raise if incompatible value types are used.