bpo-43945: [Enum] Deprecate non-standard mixin format() behavior (GH-25649)

In 3.12 the enum member, not the member's value, will be used for
format() calls.  Format specifiers can be used to retain the current
display of enum members:

Example enumeration:

    class Color(IntEnum):
        RED = 1
        GREEN = 2
        BLUE = 3

Current behavior:

    f'{Color.RED}'  -->  '1'

Future behavior:

    f'{Color.RED}'  --> 'RED'

Using d specifier:

    f'{Color.RED:d}'  --> '1'

Using specifiers can be done now and is future-compatible.
This commit is contained in:
Ethan Furman 2021-04-26 22:42:57 -07:00 committed by GitHub
parent cfe523b492
commit 5987b8c463
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 0 deletions

View file

@ -528,6 +528,14 @@ class TestEnum(unittest.TestCase):
self.assertEqual(str(TestFloat.one), 'one')
self.assertEqual('{}'.format(TestFloat.one), 'TestFloat success!')
@unittest.skipUnless(
sys.version_info[:2] < (3, 12),
'mixin-format now uses member instead of member.value',
)
def test_mixin_format_warning(self):
with self.assertWarns(DeprecationWarning):
self.assertEqual(f'{self.Grades.B}', '4')
def assertFormatIsValue(self, spec, member):
self.assertEqual(spec.format(member), spec.format(member.value))