Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-91456: Fix issue affecting the use of auto() alongside aliases in Enums#91457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ethanfurman merged 7 commits intopython:mainfromoscar-LT:fix-issue-91456
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletionsDoc/library/enum.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand Down
34 changes: 26 additions & 8 deletionsLib/enum.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1205,21 +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_value: thelast value assigned or None
last_value: thelist 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
Expand Down
65 changes: 56 additions & 9 deletionsLib/test/test_enum.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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):
Expand All@@ -3991,6 +4022,22 @@ 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()
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()
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -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.

[8]ページ先頭

©2009-2025 Movatter.jp