@@ -38,6 +38,25 @@ def load_tests(loader, tests, ignore):
38
38
))
39
39
return tests
40
40
41
+ def reraise_if_not_enum (* enum_types_or_exceptions ):
42
+ from functools import wraps
43
+
44
+ def decorator (func ):
45
+ @wraps (func )
46
+ def inner (* args , ** kwargs ):
47
+ excs = [
48
+ e
49
+ for e in enum_types_or_exceptions
50
+ if isinstance (e , Exception )
51
+ ]
52
+ if len (excs ) == 1 :
53
+ raise excs [0 ]
54
+ elif excs :
55
+ raise ExceptionGroup ('Enum Exceptions' , excs )
56
+ return func (* args , ** kwargs )
57
+ return inner
58
+ return decorator
59
+
41
60
MODULE = __name__
42
61
SHORT_MODULE = MODULE .split ('.' )[- 1 ]
43
62
@@ -75,30 +94,42 @@ class FlagStooges(Flag):
75
94
except Exception as exc :
76
95
FlagStooges = exc
77
96
78
- class FlagStoogesWithZero (Flag ):
79
- NOFLAG = 0
80
- LARRY = 1
81
- CURLY = 2
82
- MOE = 4
83
- BIG = 389
84
-
85
- class IntFlagStooges (IntFlag ):
86
- LARRY = 1
87
- CURLY = 2
88
- MOE = 4
89
- BIG = 389
90
-
91
- class IntFlagStoogesWithZero (IntFlag ):
92
- NOFLAG = 0
93
- LARRY = 1
94
- CURLY = 2
95
- MOE = 4
96
- BIG = 389
97
+ try :
98
+ class FlagStoogesWithZero (Flag ):
99
+ NOFLAG = 0
100
+ LARRY = 1
101
+ CURLY = 2
102
+ MOE = 4
103
+ BIG = 389
104
+ except Exception as exc :
105
+ FlagStoogesWithZero = exc
106
+
107
+ try :
108
+ class IntFlagStooges (IntFlag ):
109
+ LARRY = 1
110
+ CURLY = 2
111
+ MOE = 4
112
+ BIG = 389
113
+ except Exception as exc :
114
+ IntFlagStooges = exc
115
+
116
+ try :
117
+ class IntFlagStoogesWithZero (IntFlag ):
118
+ NOFLAG = 0
119
+ LARRY = 1
120
+ CURLY = 2
121
+ MOE = 4
122
+ BIG = 389
123
+ except Exception as exc :
124
+ IntFlagStoogesWithZero = exc
97
125
98
126
# for pickle test and subclass tests
99
- class Name (StrEnum ):
100
- BDFL = 'Guido van Rossum'
101
- FLUFL = 'Barry Warsaw'
127
+ try :
128
+ class Name (StrEnum ):
129
+ BDFL = 'Guido van Rossum'
130
+ FLUFL = 'Barry Warsaw'
131
+ except Exception as exc :
132
+ Name = exc
102
133
103
134
try :
104
135
Question = Enum ('Question' , 'who what when where why' , module = __name__ )
@@ -204,26 +235,35 @@ def __get__(self, instance, ownerclass):
204
235
205
236
# for global repr tests
206
237
207
- @enum .global_enum
208
- class HeadlightsK (IntFlag , boundary = enum .KEEP ):
209
- OFF_K = 0
210
- LOW_BEAM_K = auto ()
211
- HIGH_BEAM_K = auto ()
212
- FOG_K = auto ()
238
+ try :
239
+ @enum .global_enum
240
+ class HeadlightsK (IntFlag , boundary = enum .KEEP ):
241
+ OFF_K = 0
242
+ LOW_BEAM_K = auto ()
243
+ HIGH_BEAM_K = auto ()
244
+ FOG_K = auto ()
245
+ except Exception as exc :
246
+ HeadlightsK = exc
213
247
214
248
215
- @enum .global_enum
216
- class HeadlightsC (IntFlag , boundary = enum .CONFORM ):
217
- OFF_C = 0
218
- LOW_BEAM_C = auto ()
219
- HIGH_BEAM_C = auto ()
220
- FOG_C = auto ()
249
+ try :
250
+ @enum .global_enum
251
+ class HeadlightsC (IntFlag , boundary = enum .CONFORM ):
252
+ OFF_C = 0
253
+ LOW_BEAM_C = auto ()
254
+ HIGH_BEAM_C = auto ()
255
+ FOG_C = auto ()
256
+ except Exception as exc :
257
+ HeadlightsC = exc
221
258
222
259
223
- @enum .global_enum
224
- class NoName (Flag ):
225
- ONE = 1
226
- TWO = 2
260
+ try :
261
+ @enum .global_enum
262
+ class NoName (Flag ):
263
+ ONE = 1
264
+ TWO = 2
265
+ except Exception as exc :
266
+ NoName = exc
227
267
228
268
229
269
# tests
@@ -1124,9 +1164,8 @@ def red(self):
1124
1164
green = 2
1125
1165
blue = 3
1126
1166
1167
+ @reraise_if_not_enum (Theory )
1127
1168
def test_enum_function_with_qualname (self ):
1128
- if isinstance (Theory , Exception ):
1129
- raise Theory
1130
1169
self .assertEqual (Theory .__qualname__ , 'spanish_inquisition' )
1131
1170
1132
1171
def test_enum_of_types (self ):
@@ -1355,6 +1394,7 @@ class MyUnBrokenEnum(UnBrokenInt, Enum):
1355
1394
test_pickle_dump_load (self .assertIs , MyUnBrokenEnum .I )
1356
1395
test_pickle_dump_load (self .assertIs , MyUnBrokenEnum )
1357
1396
1397
+ @reraise_if_not_enum (FloatStooges )
1358
1398
def test_floatenum_fromhex (self ):
1359
1399
h = float .hex (FloatStooges .MOE .value )
1360
1400
self .assertIs (FloatStooges .fromhex (h ), FloatStooges .MOE )
@@ -1475,6 +1515,7 @@ class ThreePart(Enum):
1475
1515
self .assertIs (ThreePart ((3 , 3.0 , 'three' )), ThreePart .THREE )
1476
1516
self .assertIs (ThreePart (3 , 3.0 , 'three' ), ThreePart .THREE )
1477
1517
1518
+ @reraise_if_not_enum (IntStooges )
1478
1519
def test_intenum_from_bytes (self ):
1479
1520
self .assertIs (IntStooges .from_bytes (b'\x00 \x03 ' , 'big' ), IntStooges .MOE )
1480
1521
with self .assertRaises (ValueError ):
@@ -1503,33 +1544,28 @@ def repr(self):
1503
1544
class Huh (MyStr , MyInt , Enum ):
1504
1545
One = 1
1505
1546
1547
+ @reraise_if_not_enum (Stooges )
1506
1548
def test_pickle_enum (self ):
1507
- if isinstance (Stooges , Exception ):
1508
- raise Stooges
1509
1549
test_pickle_dump_load (self .assertIs , Stooges .CURLY )
1510
1550
test_pickle_dump_load (self .assertIs , Stooges )
1511
1551
1552
+ @reraise_if_not_enum (IntStooges )
1512
1553
def test_pickle_int (self ):
1513
- if isinstance (IntStooges , Exception ):
1514
- raise IntStooges
1515
1554
test_pickle_dump_load (self .assertIs , IntStooges .CURLY )
1516
1555
test_pickle_dump_load (self .assertIs , IntStooges )
1517
1556
1557
+ @reraise_if_not_enum (FloatStooges )
1518
1558
def test_pickle_float (self ):
1519
- if isinstance (FloatStooges , Exception ):
1520
- raise FloatStooges
1521
1559
test_pickle_dump_load (self .assertIs , FloatStooges .CURLY )
1522
1560
test_pickle_dump_load (self .assertIs , FloatStooges )
1523
1561
1562
+ @reraise_if_not_enum (Answer )
1524
1563
def test_pickle_enum_function (self ):
1525
- if isinstance (Answer , Exception ):
1526
- raise Answer
1527
1564
test_pickle_dump_load (self .assertIs , Answer .him )
1528
1565
test_pickle_dump_load (self .assertIs , Answer )
1529
1566
1567
+ @reraise_if_not_enum (Question )
1530
1568
def test_pickle_enum_function_with_module (self ):
1531
- if isinstance (Question , Exception ):
1532
- raise Question
1533
1569
test_pickle_dump_load (self .assertIs , Question .who )
1534
1570
test_pickle_dump_load (self .assertIs , Question )
1535
1571
@@ -1592,9 +1628,8 @@ class Season(Enum):
1592
1628
[Season .SUMMER , Season .WINTER , Season .AUTUMN , Season .SPRING ],
1593
1629
)
1594
1630
1631
+ @reraise_if_not_enum (Name )
1595
1632
def test_subclassing (self ):
1596<
EED3
/code>
- if isinstance (Name , Exception ):
1597
- raise Name
1598
1633
self .assertEqual (Name .BDFL , 'Guido van Rossum' )
1599
1634
self .assertTrue (Name .BDFL , Name ('Guido van Rossum' ))
1600
1635
self .assertIs (Name .BDFL , getattr (Name , 'BDFL' ))
@@ -3330,9 +3365,13 @@ def test_programatic_function_from_dict(self):
3330
3365
self .assertIn (e , Perm )
3331
3366
self .assertIs (type (e ), Perm )
3332
3367
3368
+ @reraise_if_not_enum (
3369
+ FlagStooges ,
3370
+ FlagStoogesWithZero ,
3371
+ IntFlagStooges ,
3372
+ IntFlagStoogesWithZero ,
3373
+ )
3333
3374
def test_pickle (self ):
3334
- if isinstance (FlagStooges , Exception ):
3335
- raise FlagStooges
3336
3375
test_pickle_dump_load (self .assertIs , FlagStooges .CURLY )
3337
3376
test_pickle_dump_load (self .assertEqual ,
3338
3377
FlagStooges .CURLY | FlagStooges .MOE )
@@ -3637,6 +3676,7 @@ def test_type(self):
3637
3676
self .assertTrue (isinstance (Open .WO | Open .RW , Open ))
3638
3677
self .assertEqual (Open .WO | Open .RW , 3 )
3639
3678
3679
+ @reraise_if_not_enum (HeadlightsK )
3640
3680
def test_global_repr_keep (self ):
3641
3681
self .assertEqual (
3642
3682
repr (HeadlightsK (0 )),
@@ -3651,6 +3691,7 @@ def test_global_repr_keep(self):
3651
3691
'%(m)s.HeadlightsK(8)' % {'m' : SHORT_MODULE },
3652
3692
)
3653
3693
3694
+ @reraise_if_not_enum (HeadlightsC )
3654
3695
def test_global_repr_conform1 (self ):
3655
3696
self .assertEqual (
3656
3697
repr (HeadlightsC (0 )),
@@ -3665,6 +3706,7 @@ def test_global_repr_conform1(self):
3665
3706
'%(m)s.OFF_C' % {'m' : SHORT_MODULE },
3666
3707
)
3667
3708
3709
1CF5
+ @reraise_if_not_enum (NoName )
3668
3710
def test_global_enum_str (self ):
3669
3711
self .assertEqual (str (NoName .ONE & NoName .TWO ), 'NoName(0)' )
3670
3712
self .assertEqual (str (NoName (0 )), 'NoName(0)' )
0 commit comments