Skip to content

Commit 8d59d25

Browse files
authored
bpo-46301: [Enum] test uncomparable values in _convert_ (GH-30472)
add tests that cover different types, and same non-comparable types
1 parent b6aa38f commit 8d59d25

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Lib/test/test_enum.py

+35
Original file line numberDiff line numberDiff line change
@@ -4440,6 +4440,15 @@ def test__all__(self):
44404440
CONVERT_STRING_TEST_NAME_E = 5
44414441
CONVERT_STRING_TEST_NAME_F = 5
44424442

4443+
# We also need values that cannot be compared:
4444+
UNCOMPARABLE_A = 5
4445+
UNCOMPARABLE_C = (9, 1) # naming order is broken on purpose
4446+
UNCOMPARABLE_B = 'value'
4447+
4448+
COMPLEX_C = 1j
4449+
COMPLEX_A = 2j
4450+
COMPLEX_B = 3j
4451+
44434452
class TestIntEnumConvert(unittest.TestCase):
44444453
def setUp(self):
44454454
# Reset the module-level test variables to their original integer
@@ -4477,6 +4486,32 @@ def test_convert(self):
44774486
and name not in dir(IntEnum)],
44784487
[], msg='Names other than CONVERT_TEST_* found.')
44794488

4489+
def test_convert_uncomparable(self):
4490+
uncomp = enum.Enum._convert_(
4491+
'Uncomparable',
4492+
MODULE,
4493+
filter=lambda x: x.startswith('UNCOMPARABLE_'),
4494+
)
4495+
4496+
# Should be ordered by `name` only:
4497+
self.assertEqual(
4498+
list(uncomp),
4499+
[uncomp.UNCOMPARABLE_A, uncomp.UNCOMPARABLE_B, uncomp.UNCOMPARABLE_C],
4500+
)
4501+
4502+
def test_convert_complex(self):
4503+
uncomp = enum.Enum._convert_(
4504+
'Uncomparable',
4505+
MODULE,
4506+
filter=lambda x: x.startswith('COMPLEX_'),
4507+
)
4508+
4509+
# Should be ordered by `name` only:
4510+
self.assertEqual(
4511+
list(uncomp),
4512+
[uncomp.COMPLEX_A, uncomp.COMPLEX_B, uncomp.COMPLEX_C],
4513+
)
4514+
44804515
@unittest.skipUnless(python_version == (3, 8),
44814516
'_convert was deprecated in 3.8')
44824517
def test_convert_warn(self):

0 commit comments

Comments
 (0)