))
return tests
+def reraise_if_not_enum(*enum_types_or_exceptions):
+ from functools import wraps
+
+ def decorator(func):
+ @wraps(func)
+ def inner(*args, **kwargs):
+ excs = [
+ e
+ for e in enum_types_or_exceptions
+ if isinstance(e, Exception)
+ ]
+ if len(excs) == 1:
+ raise excs[0]
+ elif excs:
+ raise ExceptionGroup('Enum Exceptions', excs)
+ return func(*args, **kwargs)
+ return inner
+ return decorator
+
MODULE = __name__
SHORT_MODULE = MODULE.split('.')[-1]
except Exception as exc:
FlagStooges = exc
-class FlagStoogesWithZero(Flag):
- NOFLAG = 0
- LARRY = 1
- CURLY = 2
- MOE = 4
- BIG = 389
-
-class IntFlagStooges(IntFlag):
- LARRY = 1
- CURLY = 2
- MOE = 4
- BIG = 389
-
-class IntFlagStoogesWithZero(IntFlag):
- NOFLAG = 0
- LARRY = 1
- CURLY = 2
- MOE = 4
- BIG = 389
+try:
+ class FlagStoogesWithZero(Flag):
+ NOFLAG = 0
+ LARRY = 1
+ CURLY = 2
+ MOE = 4
+ BIG = 389
+except Exception as exc:
+ FlagStoogesWithZero = exc
+
+try:
+ class IntFlagStooges(IntFlag):
+ LARRY = 1
+ CURLY = 2
+ MOE = 4
+ BIG = 389
+except Exception as exc:
+ IntFlagStooges = exc
+
+try:
+ class IntFlagStoogesWithZero(IntFlag):
+ NOFLAG = 0
+ LARRY = 1
+ CURLY = 2
+ MOE = 4
+ BIG = 389
+except Exception as exc:
+ IntFlagStoogesWithZero = exc
# for pickle test and subclass tests
-class Name(StrEnum):
- BDFL = 'Guido van Rossum'
- FLUFL = 'Barry Warsaw'
+try:
+ class Name(StrEnum):
+ BDFL = 'Guido van Rossum'
+ FLUFL = 'Barry Warsaw'
+except Exception as exc:
+ Name = exc
try:
Question = Enum('Question', 'who what when where why', module=__name__)
# for global repr tests
-@enum.global_enum
-class HeadlightsK(IntFlag, boundary=enum.KEEP):
- OFF_K = 0
- LOW_BEAM_K = auto()
- HIGH_BEAM_K = auto()
- FOG_K = auto()
+try:
+ @enum.global_enum
+ class HeadlightsK(IntFlag, boundary=enum.KEEP):
+ OFF_K = 0
+ LOW_BEAM_K = auto()
+ HIGH_BEAM_K = auto()
+ FOG_K = auto()
+except Exception as exc:
+ HeadlightsK = exc
-@enum.global_enum
-class HeadlightsC(IntFlag, boundary=enum.CONFORM):
- OFF_C = 0
- LOW_BEAM_C = auto()
- HIGH_BEAM_C = auto()
- FOG_C = auto()
+try:
+ @enum.global_enum
+ class HeadlightsC(IntFlag, boundary=enum.CONFORM):
+ OFF_C = 0
+ LOW_BEAM_C = auto()
+ HIGH_BEAM_C = auto()
+ FOG_C = auto()
+except Exception as exc:
+ HeadlightsC = exc
-@enum.global_enum
-class NoName(Flag):
- ONE = 1
- TWO = 2
+try:
+ @enum.global_enum
+ class NoName(Flag):
+ ONE = 1
+ TWO = 2
+except Exception as exc:
+ NoName = exc
# tests
green = 2
blue = 3
+ @reraise_if_not_enum(Theory)
def test_enum_function_with_qualname(self):
- if isinstance(Theory, Exception):
- raise Theory
self.assertEqual(Theory.__qualname__, 'spanish_inquisition')
def test_enum_of_types(self):
test_pickle_dump_load(self.assertIs, MyUnBrokenEnum.I)
test_pickle_dump_load(self.assertIs, MyUnBrokenEnum)
+ @reraise_if_not_enum(FloatStooges)
def test_floatenum_fromhex(self):
h = float.hex(FloatStooges.MOE.value)
self.assertIs(FloatStooges.fromhex(h), FloatStooges.MOE)
self.assertIs(ThreePart((3, 3.0, 'three')), ThreePart.THREE)
self.assertIs(ThreePart(3, 3.0, 'three'), ThreePart.THREE)
+ @reraise_if_not_enum(IntStooges)
def test_intenum_from_bytes(self):
self.assertIs(IntStooges.from_bytes(b'\x00\x03', 'big'), IntStooges.MOE)
with self.assertRaises(ValueError):
class Huh(MyStr, MyInt, Enum):
One = 1
+ @reraise_if_not_enum(Stooges)
def test_pickle_enum(self):
- if isinstance(Stooges, Exception):
- raise Stooges
test_pickle_dump_load(self.assertIs, Stooges.CURLY)
test_pickle_dump_load(self.assertIs, Stooges)
+ @reraise_if_not_enum(IntStooges)
def test_pickle_int(self):
- if isinstance(IntStooges, Exception):
- raise IntStooges
test_pickle_dump_load(self.assertIs, IntStooges.CURLY)
test_pickle_dump_load(self.assertIs, IntStooges)
+ @reraise_if_not_enum(FloatStooges)
def test_pickle_float(self):
- if isinstance(FloatStooges, Exception):
- raise FloatStooges
test_pickle_dump_load(self.assertIs, FloatStooges.CURLY)
test_pickle_dump_load(self.assertIs, FloatStooges)
+ @reraise_if_not_enum(Answer)
def test_pickle_enum_function(self):
- if isinstance(Answer, Exception):
- raise Answer
test_pickle_dump_load(self.assertIs, Answer.him)
test_pickle_dump_load(self.assertIs, Answer)
+ @reraise_if_not_enum(Question)
def test_pickle_enum_function_with_module(self):
- if isinstance(Question, Exception):
- raise Question
test_pickle_dump_load(self.assertIs, Question.who)
test_pickle_dump_load(self.assertIs, Question)
[Season.SUMMER, Season.WINTER, Season.AUTUMN, Season.SPRING],
)
+ @reraise_if_not_enum(Name)
def test_subclassing(self):
- if isinstance(Name, Exception):
- raise Name
self.assertEqual(Name.BDFL, 'Guido van Rossum')
self.assertTrue(Name.BDFL, Name('Guido van Rossum'))
self.assertIs(Name.BDFL, getattr(Name, 'BDFL'))
self.assertIn(e, Perm)
self.assertIs(type(e), Perm)
+ @reraise_if_not_enum(
+ FlagStooges,
+ FlagStoogesWithZero,
+ IntFlagStooges,
+ IntFlagStoogesWithZero,
+ )
def test_pickle(self):
- if isinstance(FlagStooges, Exception):
- raise FlagStooges
test_pickle_dump_load(self.assertIs, FlagStooges.CURLY)
test_pickle_dump_load(self.assertEqual,
FlagStooges.CURLY|FlagStooges.MOE)
self.assertTrue(isinstance(Open.WO | Open.RW, Open))
self.assertEqual(Open.WO | Open.RW, 3)
+ @reraise_if_not_enum(HeadlightsK)
def test_global_repr_keep(self):
self.assertEqual(
repr(HeadlightsK(0)),
'%(m)s.HeadlightsK(8)' % {'m': SHORT_MODULE},
)
+ @reraise_if_not_enum(HeadlightsC)
def test_global_repr_conform1(self):
self.assertEqual(
repr(HeadlightsC(0)),
'%(m)s.OFF_C' % {'m': SHORT_MODULE},
)
+ @reraise_if_not_enum(NoName)
def test_global_enum_str(self):
self.assertEqual(str(NoName.ONE & NoName.TWO), 'NoName(0)')
self.assertEqual(str(NoName(0)), 'NoName(0)')