From da500c2b4a173cfd49ee40b216690ce6b54e89ba Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sat, 8 Jan 2022 02:16:16 +0300 Subject: [PATCH 1/2] bpo-46301: cover uncomparable values in `Enum._convert_` --- Lib/test/test_enum.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index 2b3eac56865b1e..f21d6861ad2636 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -4440,6 +4440,14 @@ def test__all__(self): CONVERT_STRING_TEST_NAME_E = 5 CONVERT_STRING_TEST_NAME_F = 5 +# We also need values that cannot be compared: +class _Uncomparable: + ... + +UNCOMPARABLE_A = _Uncomparable() +UNCOMPARABLE_C = _Uncomparable() # order is broken on purpose +UNCOMPARABLE_B = _Uncomparable() + class TestIntEnumConvert(unittest.TestCase): def setUp(self): # Reset the module-level test variables to their original integer @@ -4477,6 +4485,19 @@ def test_convert(self): and name not in dir(IntEnum)], [], msg='Names other than CONVERT_TEST_* found.') + def test_convert_uncomparable(self): + uncomp = enum.Enum._convert_( + 'Uncomparable', + MODULE, + filter=lambda x: x.startswith('UNCOMPARABLE_'), + ) + + # Should be ordered by `name` only: + self.assertEqual( + list(uncomp), + [uncomp.UNCOMPARABLE_A, uncomp.UNCOMPARABLE_B, uncomp.UNCOMPARABLE_C], + ) + @unittest.skipUnless(python_version == (3, 8), '_convert was deprecated in 3.8') def test_convert_warn(self): From 21d5672a405756c93318f0cc9822c8494ae87c7a Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sat, 8 Jan 2022 11:08:38 +0300 Subject: [PATCH 2/2] Address review --- Lib/test/test_enum.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index f21d6861ad2636..7e919fb9b42639 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -4441,12 +4441,13 @@ def test__all__(self): CONVERT_STRING_TEST_NAME_F = 5 # We also need values that cannot be compared: -class _Uncomparable: - ... +UNCOMPARABLE_A = 5 +UNCOMPARABLE_C = (9, 1) # naming order is broken on purpose +UNCOMPARABLE_B = 'value' -UNCOMPARABLE_A = _Uncomparable() -UNCOMPARABLE_C = _Uncomparable() # order is broken on purpose -UNCOMPARABLE_B = _Uncomparable() +COMPLEX_C = 1j +COMPLEX_A = 2j +COMPLEX_B = 3j class TestIntEnumConvert(unittest.TestCase): def setUp(self): @@ -4498,6 +4499,19 @@ def test_convert_uncomparable(self): [uncomp.UNCOMPARABLE_A, uncomp.UNCOMPARABLE_B, uncomp.UNCOMPARABLE_C], ) + def test_convert_complex(self): + uncomp = enum.Enum._convert_( + 'Uncomparable', + MODULE, + filter=lambda x: x.startswith('COMPLEX_'), + ) + + # Should be ordered by `name` only: + self.assertEqual( + list(uncomp), + [uncomp.COMPLEX_A, uncomp.COMPLEX_B, uncomp.COMPLEX_C], + ) + @unittest.skipUnless(python_version == (3, 8), '_convert was deprecated in 3.8') def test_convert_warn(self):