From 5987b8c463892e0ab7a63cdae92f34b5eb79732d Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Mon, 26 Apr 2021 22:42:57 -0700 Subject: 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. --- Lib/enum.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'Lib/enum.py') diff --git a/Lib/enum.py b/Lib/enum.py index ca6aff6f82..bcf411c6b6 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -1005,6 +1005,14 @@ class Enum(metaclass=EnumType): val = str(self) # mix-in branch else: + import warnings + warnings.warn( + "in 3.12 format() will use the enum member, not the enum member's value;\n" + "use a format specifier, such as :d for an IntEnum member, to maintain" + "the current display", + DeprecationWarning, + stacklevel=2, + ) cls = self._member_type_ val = self._value_ return cls.__format__(val, format_spec) -- cgit v1.2.1