]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46301: [Enum] test uncomparable values in `_convert_` (GH-30472)
authorNikita Sobolev <mail@sobolevn.me>
Sat, 8 Jan 2022 19:43:42 +0000 (22:43 +0300)
committerGitHub <noreply@github.com>
Sat, 8 Jan 2022 19:43:42 +0000 (11:43 -0800)
add tests that cover different types, and same non-comparable types

Lib/test/test_enum.py

index 2b3eac56865b1ed769cb25226cd3e142904847f2..7e919fb9b426396184f4964eb304a2e1d4a56cd3 100644 (file)
@@ -4440,6 +4440,15 @@ CONVERT_STRING_TEST_NAME_A = 5  # This one should sort first.
 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 @@ class TestIntEnumConvert(unittest.TestCase):
                           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):