mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
gh-104764: [Enum] fix 3.13-specific tests (GH-104779)
This commit is contained in:
parent
2e5d8a90aa
commit
5ecd8c85f9
2 changed files with 19 additions and 43 deletions
44
Lib/enum.py
44
Lib/enum.py
|
@ -388,16 +388,8 @@ class _EnumDict(dict):
|
||||||
|
|
||||||
Single underscore (sunder) names are reserved.
|
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):
|
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
|
pass
|
||||||
elif _is_sunder(key):
|
elif _is_sunder(key):
|
||||||
if key not in (
|
if key not in (
|
||||||
|
@ -440,10 +432,9 @@ class _EnumDict(dict):
|
||||||
value = value.value
|
value = value.value
|
||||||
elif _is_descriptor(value):
|
elif _is_descriptor(value):
|
||||||
pass
|
pass
|
||||||
# TODO: uncomment next three lines in 3.13
|
elif _is_internal_class(self._cls_name, value):
|
||||||
# elif _is_internal_class(self._cls_name, value):
|
# do nothing, name will be a normal attribute
|
||||||
# # do nothing, name will be a normal attribute
|
pass
|
||||||
# pass
|
|
||||||
else:
|
else:
|
||||||
if key in self:
|
if key in self:
|
||||||
# enum overwriting a descriptor?
|
# enum overwriting a descriptor?
|
||||||
|
@ -1169,28 +1160,13 @@ class Enum(metaclass=EnumType):
|
||||||
if not last_values:
|
if not last_values:
|
||||||
return start
|
return start
|
||||||
try:
|
try:
|
||||||
last = last_values[-1]
|
last_value = sorted(last_values).pop()
|
||||||
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
|
|
||||||
except TypeError:
|
except TypeError:
|
||||||
import warnings
|
raise TypeError('unable to sort non-numeric values') from None
|
||||||
warnings.warn(
|
try:
|
||||||
"In 3.13 the default `auto()`/`_generate_next_value_` will require all values to be sortable and support adding +1\n"
|
return last_value + 1
|
||||||
"and the value returned will be the largest value in the enum incremented by 1",
|
except TypeError:
|
||||||
DeprecationWarning,
|
raise TypeError('unable to increment %r' % (last_value, )) from None
|
||||||
stacklevel=3,
|
|
||||||
)
|
|
||||||
for v in last_values:
|
|
||||||
try:
|
|
||||||
return v + 1
|
|
||||||
except TypeError:
|
|
||||||
pass
|
|
||||||
return start
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _missing_(cls, value):
|
def _missing_(cls, value):
|
||||||
|
|
|
@ -1090,7 +1090,7 @@ class TestSpecial(unittest.TestCase):
|
||||||
)
|
)
|
||||||
|
|
||||||
@unittest.skipIf(
|
@unittest.skipIf(
|
||||||
python_version < (3, 14),
|
python_version < (3, 13),
|
||||||
'inner classes are still members',
|
'inner classes are still members',
|
||||||
)
|
)
|
||||||
def test_nested_classes_in_enum_are_not_members(self):
|
def test_nested_classes_in_enum_are_not_members(self):
|
||||||
|
@ -4261,21 +4261,21 @@ class TestInternals(unittest.TestCase):
|
||||||
self.assertEqual(Color.green.value, 3)
|
self.assertEqual(Color.green.value, 3)
|
||||||
|
|
||||||
@unittest.skipIf(
|
@unittest.skipIf(
|
||||||
python_version < (3, 14),
|
python_version < (3, 13),
|
||||||
'mixed types with auto() will raise in the future',
|
'mixed types with auto() will raise in 3.13',
|
||||||
)
|
)
|
||||||
def test_auto_garbage_fail(self):
|
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):
|
class Color(Enum):
|
||||||
red = 'red'
|
red = 'red'
|
||||||
blue = auto()
|
blue = auto()
|
||||||
|
|
||||||
@unittest.skipIf(
|
@unittest.skipIf(
|
||||||
python_version < (3, 14),
|
python_version < (3, 13),
|
||||||
'mixed types with auto() will raise in the future',
|
'mixed types with auto() will raise in 3.13',
|
||||||
)
|
)
|
||||||
def test_auto_garbage_corrected_fail(self):
|
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):
|
class Color(Enum):
|
||||||
red = 'red'
|
red = 'red'
|
||||||
blue = 2
|
blue = 2
|
||||||
|
@ -4303,8 +4303,8 @@ class TestInternals(unittest.TestCase):
|
||||||
self.assertEqual(Color.blue.value, 'blue')
|
self.assertEqual(Color.blue.value, 'blue')
|
||||||
|
|
||||||
@unittest.skipIf(
|
@unittest.skipIf(
|
||||||
python_version < (3, 14),
|
python_version < (3, 13),
|
||||||
'auto() will return highest value + 1 in the future',
|
'auto() will return highest value + 1 in 3.13',
|
||||||
)
|
)
|
||||||
def test_auto_with_aliases(self):
|
def test_auto_with_aliases(self):
|
||||||
class Color(Enum):
|
class Color(Enum):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue