mirror of
https://github.com/python/cpython.git
synced 2025-09-20 07:31:10 +00:00
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>
(cherry picked from commit fb1e9506c1
)
Co-authored-by: Oscar R <89599049+oscar-LT@users.noreply.github.com>
This commit is contained in:
parent
00a25f87f3
commit
321acd4138
4 changed files with 89 additions and 17 deletions
|
@ -761,6 +761,10 @@ Utilities and Decorators
|
||||||
``_generate_next_value_`` can be overridden to customize the values used by
|
``_generate_next_value_`` can be overridden to customize the values used by
|
||||||
*auto*.
|
*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
|
.. decorator:: property
|
||||||
|
|
||||||
A decorator similar to the built-in *property*, but specifically for
|
A decorator similar to the built-in *property*, but specifically for
|
||||||
|
|
34
Lib/enum.py
34
Lib/enum.py
|
@ -1218,21 +1218,39 @@ class Enum(metaclass=EnumType):
|
||||||
def __init__(self, *args, **kwds):
|
def __init__(self, *args, **kwds):
|
||||||
pass
|
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.
|
Generate the next value when not given.
|
||||||
|
|
||||||
name: the name of the member
|
name: the name of the member
|
||||||
start: the initial start value or None
|
start: the initial start value or None
|
||||||
count: the number of existing members
|
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):
|
if not last_value:
|
||||||
try:
|
return start
|
||||||
return last_value + 1
|
try:
|
||||||
except TypeError:
|
last = last_value[-1]
|
||||||
pass
|
last_value.sort()
|
||||||
else:
|
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
|
return start
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
|
@ -3972,23 +3972,54 @@ class TestInternals(unittest.TestCase):
|
||||||
self.assertEqual(Color.blue.value, 'blue')
|
self.assertEqual(Color.blue.value, 'blue')
|
||||||
self.assertEqual(Color.green.value, 'green')
|
self.assertEqual(Color.green.value, 'green')
|
||||||
|
|
||||||
def test_auto_garbage(self):
|
@unittest.skipIf(
|
||||||
class Color(Enum):
|
python_version >= (3, 13),
|
||||||
red = 'red'
|
'mixed types with auto() no longer supported',
|
||||||
blue = auto()
|
)
|
||||||
|
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)
|
self.assertEqual(Color.blue.value, 1)
|
||||||
|
|
||||||
def test_auto_garbage_corrected(self):
|
@unittest.skipIf(
|
||||||
class Color(Enum):
|
python_version >= (3, 13),
|
||||||
red = 'red'
|
'mixed types with auto() no longer supported',
|
||||||
blue = 2
|
)
|
||||||
green = auto()
|
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(list(Color), [Color.red, Color.blue, Color.green])
|
||||||
self.assertEqual(Color.red.value, 'red')
|
self.assertEqual(Color.red.value, 'red')
|
||||||
self.assertEqual(Color.blue.value, 2)
|
self.assertEqual(Color.blue.value, 2)
|
||||||
self.assertEqual(Color.green.value, 3)
|
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):
|
def test_auto_order(self):
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
class Color(Enum):
|
class Color(Enum):
|
||||||
|
@ -4010,6 +4041,22 @@ class TestInternals(unittest.TestCase):
|
||||||
self.assertEqual(Color.red.value, 'pathological case')
|
self.assertEqual(Color.red.value, 'pathological case')
|
||||||
self.assertEqual(Color.blue.value, 'blue')
|
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()
|
||||||
|
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):
|
def test_duplicate_auto(self):
|
||||||
class Dupes(Enum):
|
class Dupes(Enum):
|
||||||
first = primero = auto()
|
first = primero = auto()
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
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.
|
Loading…
Add table
Add a link
Reference in a new issue