summaryrefslogtreecommitdiffstats
path: root/Lib/enum.py
diff options
context:
space:
mode:
authorOscar R <89599049+oscar-LT@users.noreply.github.com>2022-06-23 06:20:24 (GMT)
committerGitHub <noreply@github.com>2022-06-23 06:20:24 (GMT)
commitfb1e9506c14ef32d5bec126dad6fa769c8c054f6 (patch)
tree07415da3086cd6eccf5ad0b17519ebc4b9eda80e /Lib/enum.py
parent7c439dca13435085efb2fddf9ac75e5305db7ada (diff)
downloadcpython-fb1e9506c14ef32d5bec126dad6fa769c8c054f6.zip
cpython-fb1e9506c14ef32d5bec126dad6fa769c8c054f6.tar.gz
cpython-fb1e9506c14ef32d5bec126dad6fa769c8c054f6.tar.bz2
gh-91456: [Enum] Deprecate default auto() behavior with mixed value types (GH-91457)
When used with plain Enum, auto() returns the last numeric value assigned, skipping any incompatible member values (such as strings); starting in 3.13 the default auto() for plain Enums will require all the values to be of compatible types, and will return a new value that is 1 higher than any existing value. Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
Diffstat (limited to 'Lib/enum.py')
-rw-r--r--Lib/enum.py34
1 files changed, 26 insertions, 8 deletions
diff --git a/Lib/enum.py b/Lib/enum.py
index decb601..8d0982a 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -1205,21 +1205,39 @@ class Enum(metaclass=EnumType):
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_value: the last value assigned or None
+ last_value: the list of values assigned
"""
- for last_value in reversed(last_values):
- try:
- return last_value + 1
- except TypeError:
- pass
- 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