[Python-checkins] bpo-46301: [Enum] test uncomparable values in `_convert_` (GH-30472)

ethanfurman webhook-mailer at python.org
Sat Jan 8 14:43:50 EST 2022


https://github.com/python/cpython/commit/8d59d2563b914b7208779834895c080c70cd94dd
commit: 8d59d2563b914b7208779834895c080c70cd94dd
branch: main
author: Nikita Sobolev <mail at sobolevn.me>
committer: ethanfurman <ethan at stoneleaf.us>
date: 2022-01-08T11:43:42-08:00
summary:

bpo-46301: [Enum] test uncomparable values in `_convert_` (GH-30472)

add tests that cover different types, and same non-comparable types

files:
M Lib/test/test_enum.py

diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py
index 2b3eac56865b1..7e919fb9b4263 100644
--- a/Lib/test/test_enum.py
+++ b/Lib/test/test_enum.py
@@ -4440,6 +4440,15 @@ def test__all__(self):
 CONVERT_STRING_TEST_NAME_E = 5
 CONVERT_STRING_TEST_NAME_F = 5
 
+# We also need values that cannot be compared:
+UNCOMPARABLE_A = 5
+UNCOMPARABLE_C = (9, 1)  # naming order is broken on purpose
+UNCOMPARABLE_B = 'value'
+
+COMPLEX_C = 1j
+COMPLEX_A = 2j
+COMPLEX_B = 3j
+
 class TestIntEnumConvert(unittest.TestCase):
     def setUp(self):
         # Reset the module-level test variables to their original integer
@@ -4477,6 +4486,32 @@ 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],
+        )
+
+    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):



More information about the Python-checkins mailing list