8000 gh-104764: [Enum] fix 3.13-specific tests by ethanfurman · Pull Request #104779 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-104764: [Enum] fix 3.13-specific tests #104779

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
44 changes: 10 additions & 34 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,16 +388,8 @@ def __setitem__(self, key, value):

Single underscore (sunder) names are reserved.
"""
if _is_internal_class(self._cls_name, value):
import warnings
warnings.warn(
"In 3.13 classes created inside an enum will not become a member. "
"Use the `member` decorator to keep the current behavior.",
DeprecationWarning,
stacklevel=2,
)
if _is_private(self._cls_name, key):
# also do nothing, name will be a normal attribute
# do nothing, name will be a normal attribute
pass
elif _is_sunder(key):
if key not in (
Expand Down Expand Up @@ -440,10 +432,9 @@ def __setitem__(self, key, value):
value = value.value
elif _is_descriptor(value):
pass
# TODO: uncomment next three lines in 3.13
# elif _is_internal_class(self._cls_name, value):
# # do nothing, name will be a normal attribute
# pass
elif _is_internal_class(self._cls_name, value):
# do nothing, name will be a normal attribute
pass
else:
if key in self:
# enum overwriting a descriptor?
Expand Down Expand Up @@ -1169,28 +1160,13 @@ def _generate_next_value_(name, start, count, last_values):
if not last_values:
return start
try:
last = last_values[-1]
last_values.sort()
if last == last_values[-1]:
# no difference between old and new methods
return last + 1
else:
# trigger old method (with warning)
raise TypeError
last_value = sorted(last_values).pop()
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_values:
try:
return v + 1
except TypeError:
pass
return start
raise TypeError('unable to sort non-numeric values') from None
try:
return last_value + 1
except TypeError:
raise TypeError('unable to increment %r' % (last_value, )) from None

@classmethod
def _missing_(cls, value):
Expand Down
18 changes: 9 additions & 9 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ class Inner(Enum):
)

@unittest.skipIf(
python_version < (3, 14),
python_version < (3, 13),
'inner classes are still members',
)
def test_nested_classes_in_enum_are_not_members(self):
Expand Down Expand Up @@ -4261,21 +4261,21 @@ class Color(Enum):
self.assertEqual(Color.green.value, 3)

@unittest.skipIf(
python_version < (3, 14),
'mixed types with auto() will raise in the future',
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'):
with self.assertRaisesRegex(TypeError, "unable to increment 'red'"):
class Color(Enum):
red = 'red'
blue = auto()

@unittest.skipIf(
python_version < (3, 14),
'mixed types with auto() will raise in the future',
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'):
with self.assertRaisesRegex(TypeError, 'unable to sort non-numeric values'):
class Color(Enum):
red = 'red'
blue = 2
Expand Down Expand Up @@ -4303,8 +4303,8 @@ def _generate_next_value_(name, start, count, last):
self.assertEqual(Color.blue.value, 'blue')

@unittest.skipIf(
python_version < (3, 14),
'auto() will return highest value + 1 in the future',
python_version < (3, 13),
'auto() will return highest value + 1 in 3.13',
)
def test_auto_with_aliases(self):
class Color(Enum):
Expand Down
0