diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2022-06-23 06:46:33 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-23 06:46:33 (GMT) |
commit | 321acd4138b65363be1f11e5b04f86c204b27a2f (patch) | |
tree | cef2976fe1849ee9c307b6787ec4e6272fcb803e /Lib/test | |
parent | 00a25f87f37f02cd5c0477c5c642a0a7eb322a04 (diff) | |
download | cpython-321acd4138b65363be1f11e5b04f86c204b27a2f.zip cpython-321acd4138b65363be1f11e5b04f86c204b27a2f.tar.gz cpython-321acd4138b65363be1f11e5b04f86c204b27a2f.tar.bz2 |
gh-91456: [Enum] Deprecate default auto() behavior with mixed value types (GH-91457)
When used with plain Enum, auto() returns the last numeric value assigned, skipping any incompatible member values (such as strings); starting in 3.13 the default auto() for plain Enums will require all the values to be of compatible types, and will return a new value that is 1 higher than any existing value.
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
(cherry picked from commit fb1e9506c14ef32d5bec126dad6fa769c8c054f6)
Co-authored-by: Oscar R <89599049+oscar-LT@users.noreply.github.com>
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_enum.py | 65 |
1 files changed, 56 insertions, 9 deletions
diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index 66c7767..623d265 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -3972,23 +3972,54 @@ class TestInternals(unittest.TestCase): self.assertEqual(Color.blue.value, 'blue') self.assertEqual(Color.green.value, 'green') - def test_auto_garbage(self): - class Color(Enum): - red = 'red' - blue = auto() + @unittest.skipIf( + python_version >= (3, 13), + 'mixed types with auto() no longer supported', + ) + def test_auto_garbage_ok(self): + with self.assertWarnsRegex(DeprecationWarning, 'will require all values to be sortable'): + class Color(Enum): + red = 'red' + blue = auto() self.assertEqual(Color.blue.value, 1) - def test_auto_garbage_corrected(self): - class Color(Enum): - red = 'red' - blue = 2 - green = auto() + @unittest.skipIf( + python_version >= (3, 13), + 'mixed types with auto() no longer supported', + ) + def test_auto_garbage_corrected_ok(self): + with self.assertWarnsRegex(DeprecationWarning, 'will require all values to be sortable'): + class Color(Enum): + red = 'red' + blue = 2 + green = auto() self.assertEqual(list(Color), [Color.red, Color.blue, Color.green]) self.assertEqual(Color.red.value, 'red') self.assertEqual(Color.blue.value, 2) self.assertEqual(Color.green.value, 3) + @unittest.skipIf( + python_version < (3, 13), + 'mixed types with auto() will raise in 3.13', + ) + def test_auto_garbage_fail(self): + with self.assertRaisesRegex(TypeError, 'will require all values to be sortable'): + class Color(Enum): + red = 'red' + blue = auto() + + @unittest.skipIf( + python_version < (3, 13), + 'mixed types with auto() will raise in 3.13', + ) + def test_auto_garbage_corrected_fail(self): + with self.assertRaisesRegex(TypeError, 'will require all values to be sortable'): + class Color(Enum): + red = 'red' + blue = 2 + green = auto() + def test_auto_order(self): with self.assertRaises(TypeError): class Color(Enum): @@ -4010,6 +4041,22 @@ class TestInternals(unittest.TestCase): self.assertEqual(Color.red.value, 'pathological case') self.assertEqual(Color.blue.value, 'blue') + @unittest.skipIf( + python_version < (3, 13), + 'auto() will return highest value + 1 in 3.13', + ) + def test_auto_with_aliases(self): + class Color(Enum): + red = auto() + blue = auto() + oxford = blue + crimson = red + green = auto() + self.assertIs(Color.crimson, Color.red) + self.assertIs(Color.oxford, Color.blue) + self.assertIsNot(Color.green, Color.red) + self.assertIsNot(Color.green, Color.blue) + def test_duplicate_auto(self): class Dupes(Enum): first = primero = auto() |