Skip to content

Commit 503cdc7

Browse files
authored
Revert "bpo-38659: [Enum] add _simple_enum decorator (GH-25285)" (GH-25476)
This reverts commit dbac8f4.
1 parent dbac8f4 commit 503cdc7

File tree

20 files changed

+34
-871
lines changed

20 files changed

+34
-871
lines changed

Doc/library/enum.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,3 +621,4 @@ Utilites and Decorators
621621
Traceback (most recent call last):
622622
...
623623
ValueError: duplicate values found in <enum 'Mistake'>: FOUR -> THREE
624+

Lib/ast.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import sys
2828
from _ast import *
2929
from contextlib import contextmanager, nullcontext
30-
from enum import IntEnum, auto, _simple_enum
30+
from enum import IntEnum, auto
3131

3232

3333
def parse(source, filename='<unknown>', mode='exec', *,
@@ -636,8 +636,7 @@ class Param(expr_context):
636636
# We unparse those infinities to INFSTR.
637637
_INFSTR = "1e" + repr(sys.float_info.max_10_exp + 1)
638638

639-
@_simple_enum(IntEnum)
640-
class _Precedence:
639+
class _Precedence(IntEnum):
641640
"""Precedence table that originated from python grammar."""
642641

643642
TUPLE = auto()

Lib/enum.py

Lines changed: 7 additions & 319 deletions
Large diffs are not rendered by default.

Lib/http/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
from enum import IntEnum, _simple_enum
1+
from enum import IntEnum
22

33
__all__ = ['HTTPStatus']
44

5-
6-
@_simple_enum(IntEnum)
7-
class HTTPStatus:
5+
class HTTPStatus(IntEnum):
86
"""HTTP status codes and reason phrases
97
108
Status codes from the following RFCs are all observed:

Lib/pstats.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@
2626
import marshal
2727
import re
2828

29-
from enum import StrEnum, _simple_enum
29+
from enum import Enum
3030
from functools import cmp_to_key
3131
from dataclasses import dataclass
3232
from typing import Dict
3333

3434
__all__ = ["Stats", "SortKey", "FunctionProfile", "StatsProfile"]
3535

36-
@_simple_enum(StrEnum)
37-
class SortKey:
36+
class SortKey(str, Enum):
3837
CALLS = 'calls', 'ncalls'
3938
CUMULATIVE = 'cumulative', 'cumtime'
4039
FILENAME = 'filename', 'module'

Lib/re.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@
143143
__version__ = "2.2.1"
144144

145145
@enum.global_enum
146-
@enum._simple_enum(enum.IntFlag, boundary=enum.KEEP)
147-
class RegexFlag:
146+
class RegexFlag(enum.IntFlag, boundary=enum.KEEP):
148147
ASCII = A = sre_compile.SRE_FLAG_ASCII # assume ascii "locale"
149148
IGNORECASE = I = sre_compile.SRE_FLAG_IGNORECASE # ignore case
150149
LOCALE = L = sre_compile.SRE_FLAG_LOCALE # assume current 8-bit locale

Lib/ssl.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@
9494
import os
9595
from collections import namedtuple
9696
from enum import Enum as _Enum, IntEnum as _IntEnum, IntFlag as _IntFlag
97-
from enum import _simple_enum, _test_simple_enum
9897

9998
import _ssl # if we can't import it, let the error propagate
10099

@@ -156,8 +155,7 @@
156155
_SSLv2_IF_EXISTS = getattr(_SSLMethod, 'PROTOCOL_SSLv2', None)
157156

158157

159-
@_simple_enum(_IntEnum)
160-
class TLSVersion:
158+
class TLSVersion(_IntEnum):
161159
MINIMUM_SUPPORTED = _ssl.PROTO_MINIMUM_SUPPORTED
162160
SSLv3 = _ssl.PROTO_SSLv3
163161
TLSv1 = _ssl.PROTO_TLSv1
@@ -167,8 +165,7 @@ class TLSVersion:
167165
MAXIMUM_SUPPORTED = _ssl.PROTO_MAXIMUM_SUPPORTED
168166

169167

170-
@_simple_enum(_IntEnum)
171-
class _TLSContentType:
168+
class _TLSContentType(_IntEnum):
172169
"""Content types (record layer)
173170
174171
See RFC 8446, section B.1
@@ -182,8 +179,7 @@ class _TLSContentType:
182179
INNER_CONTENT_TYPE = 0x101
183180

184181

185-
@_simple_enum(_IntEnum)
186-
class _TLSAlertType:
182+
class _TLSAlertType(_IntEnum):
187183
"""Alert types for TLSContentType.ALERT messages
188184
189185
See RFC 8466, section B.2
@@ -224,8 +220,7 @@ class _TLSAlertType:
224220
NO_APPLICATION_PROTOCOL = 120
225221

226222

227-
@_simple_enum(_IntEnum)
228-
class _TLSMessageType:
223+
class _TLSMessageType(_IntEnum):
229224
"""Message types (handshake protocol)
230225
231226
See RFC 8446, section B.3

Lib/test/test_ast.py

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import ast
22
import builtins
33
import dis
4-
import enum
54
import os
65
import sys
76
import types
@@ -699,35 +698,6 @@ def test_constant_as_name(self):
699698
with self.assertRaisesRegex(ValueError, f"Name node can't be used with '{constant}' constant"):
700699
compile(expr, "<test>", "eval")
701700

702-
def test_precedence_enum(self):
703-
class _Precedence(enum.IntEnum):
704-
"""Precedence table that originated from python grammar."""
705-
TUPLE = enum.auto()
706-
YIELD = enum.auto() # 'yield', 'yield from'
707-
TEST = enum.auto() # 'if'-'else', 'lambda'
708-
OR = enum.auto() # 'or'
709-
AND = enum.auto() # 'and'
710-
NOT = enum.auto() # 'not'
711-
CMP = enum.auto() # '<', '>', '==', '>=', '<=', '!=',
712-
# 'in', 'not in', 'is', 'is not'
713-
EXPR = enum.auto()
714-
BOR = EXPR # '|'
715-
BXOR = enum.auto() # '^'
716-
BAND = enum.auto() # '&'
717-
SHIFT = enum.auto() # '<<', '>>'
718-
ARITH = enum.auto() # '+', '-'
719-
TERM = enum.auto() # '*', '@', '/', '%', '//'
720-
FACTOR = enum.auto() # unary '+', '-', '~'
721-
POWER = enum.auto() # '**'
722-
AWAIT = enum.auto() # 'await'
723-
ATOM = enum.auto()
724-
def next(self):
725-
try:
726-
return self.__class__(self + 1)
727-
except ValueError:
728-
return self
729-
enum._test_simple_enum(_Precedence, ast._Precedence)
730-
731701

732702
class ASTHelpers_Test(unittest.TestCase):
733703
maxDiff = None

Lib/test/test_enum.py

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import threading
99
from collections import OrderedDict
1010
from enum import Enum, IntEnum, StrEnum, EnumType, Flag, IntFlag, unique, auto
11-
from enum import STRICT, CONFORM, EJECT, KEEP, _simple_enum, _test_simple_enum
11+
from enum import STRICT, CONFORM, EJECT, KEEP
1212
from io import StringIO
1313
from pickle import dumps, loads, PicklingError, HIGHEST_PROTOCOL
1414
from test import support
@@ -2511,13 +2511,10 @@ class Bizarre(Flag, boundary=KEEP):
25112511
d = 6
25122512
#
25132513
self.assertRaisesRegex(ValueError, 'invalid value: 7', Iron, 7)
2514-
#
25152514
self.assertIs(Water(7), Water.ONE|Water.TWO)
25162515
self.assertIs(Water(~9), Water.TWO)
2517-
#
25182516
self.assertEqual(Space(7), 7)
25192517
self.assertTrue(type(Space(7)) is int)
2520-
#
25212518
self.assertEqual(list(Bizarre), [Bizarre.c])
25222519
self.assertIs(Bizarre(3), Bizarre.b)
25232520
self.assertIs(Bizarre(6), Bizarre.d)
@@ -3056,20 +3053,16 @@ class Space(IntFlag, boundary=EJECT):
30563053
EIGHT = 8
30573054
self.assertIs(Space._boundary_, EJECT)
30583055
#
3059-
#
30603056
class Bizarre(IntFlag, boundary=KEEP):
30613057
b = 3
30623058
c = 4
30633059
d = 6
30643060
#
30653061
self.assertRaisesRegex(ValueError, 'invalid value: 5', Iron, 5)
3066-
#
30673062
self.assertIs(Water(7), Water.ONE|Water.TWO)
30683063
self.assertIs(Water(~9), Water.TWO)
3069-
#
30703064
self.assertEqual(Space(7), 7)
30713065
self.assertTrue(type(Space(7)) is int)
3072-
#
30733066
self.assertEqual(list(Bizarre), [Bizarre.c])
30743067
self.assertIs(Bizarre(3), Bizarre.b)
30753068
self.assertIs(Bizarre(6), Bizarre.d)
@@ -3584,41 +3577,6 @@ def test_inspect_classify_class_attrs(self):
35843577
if failed:
35853578
self.fail("result does not equal expected, see print above")
35863579

3587-
def test_test_simple_enum(self):
3588-
@_simple_enum(Enum)
3589-
class SimpleColor:
3590-
RED = 1
3591-
GREEN = 2
3592-
BLUE = 3
3593-
class CheckedColor(Enum):
3594-
RED = 1
3595-
GREEN = 2
3596-
BLUE = 3
3597-
self.assertTrue(_test_simple_enum(CheckedColor, SimpleColor) is None)
3598-
SimpleColor.GREEN._value_ = 9
3599-
self.assertRaisesRegex(
3600-
TypeError, "enum mismatch",
3601-
_test_simple_enum, CheckedColor, SimpleColor,
3602-
)
3603-
class CheckedMissing(IntFlag, boundary=KEEP):
3604-
SIXTY_FOUR = 64
3605-
ONE_TWENTY_EIGHT = 128
3606-
TWENTY_FORTY_EIGHT = 2048
3607-
ALL = 2048 + 128 + 64 + 12
3608-
CM = CheckedMissing
3609-
self.assertEqual(list(CheckedMissing), [CM.SIXTY_FOUR, CM.ONE_TWENTY_EIGHT, CM.TWENTY_FORTY_EIGHT])
3610-
#
3611-
@_simple_enum(IntFlag, boundary=KEEP)
3612-
class Missing:
3613-
SIXTY_FOUR = 64
3614-
ONE_TWENTY_EIGHT = 128
3615-
TWENTY_FORTY_EIGHT = 2048
3616-
ALL = 2048 + 128 + 64 + 12
3617-
M = Missing
3618-
self.assertEqual(list(CheckedMissing), [M.SIXTY_FOUR, M.ONE_TWENTY_EIGHT, M.TWENTY_FORTY_EIGHT])
3619-
#
3620-
_test_simple_enum(CheckedMissing, Missing)
3621-
36223580

36233581
class MiscTestCase(unittest.TestCase):
36243582
def test__all__(self):
@@ -3634,13 +3592,6 @@ def test__all__(self):
36343592
CONVERT_TEST_NAME_E = 5
36353593
CONVERT_TEST_NAME_F = 5
36363594

3637-
CONVERT_STRING_TEST_NAME_D = 5
3638-
CONVERT_STRING_TEST_NAME_C = 5
3639-
CONVERT_STRING_TEST_NAME_B = 5
3640-
CONVERT_STRING_TEST_NAME_A = 5 # This one should sort first.
3641-
CONVERT_STRING_TEST_NAME_E = 5
3642-
CONVERT_STRING_TEST_NAME_F = 5
3643-
36443595
class TestIntEnumConvert(unittest.TestCase):
36453596
def test_convert_value_lookup_priority(self):
36463597
test_type = enum.IntEnum._convert_(
@@ -3688,16 +3639,14 @@ def test_convert_raise(self):
36883639
filter=lambda x: x.startswith('CONVERT_TEST_'))
36893640

36903641
def test_convert_repr_and_str(self):
3691-
# reset global constants, as previous tests could have converted the
3692-
# integer values to enums
36933642
module = ('test.test_enum', '__main__')[__name__=='__main__']
36943643
test_type = enum.IntEnum._convert_(
36953644
'UnittestConvert',
36963645
module,
3697-
filter=lambda x: x.startswith('CONVERT_STRING_TEST_'))
3698-
self.assertEqual(repr(test_type.CONVERT_STRING_TEST_NAME_A), '%s.CONVERT_STRING_TEST_NAME_A' % module)
3699-
self.assertEqual(str(test_type.CONVERT_STRING_TEST_NAME_A), 'CONVERT_STRING_TEST_NAME_A')
3700-
self.assertEqual(format(test_type.CONVERT_STRING_TEST_NAME_A), '5')
3646+
filter=lambda x: x.startswith('CONVERT_TEST_'))
3647+
self.assertEqual(repr(test_type.CONVERT_TEST_NAME_A), '%s.CONVERT_TEST_NAME_A' % module)
3648+
self.assertEqual(str(test_type.CONVERT_TEST_NAME_A), 'CONVERT_TEST_NAME_A')
3649+
self.assertEqual(format(test_type.CONVERT_TEST_NAME_A), '5')
37013650

37023651
# global names for StrEnum._convert_ test
37033652
CONVERT_STR_TEST_2 = 'goodbye'

0 commit comments

Comments
 (0)