This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: [Enum] standardize format() behavior
Type: behavior Stage: resolved
Components: Versions: Python 3.11, Python 3.10
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ethan.furman Nosy List: barry, ethan.furman
Priority: normal Keywords: patch

Created on 2021-04-26 23:04 by ethan.furman, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 25649 merged ethan.furman, 2021-04-27 04:20
PR 26752 merged ethan.furman, 2021-06-16 04:31
PR 26791 merged ethan.furman, 2021-06-18 20:21
Messages (5)
msg391995 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2021-04-26 23:04
Currently, an enum with a mixed-in data type, such as IntEnum, will use that data type's `__format__` -- unless the user provides their own `__str__`, in which case the `str()` of the enum member will be used in the `format()` call.

This behavior will be deprecated in 3.10, and in 3.12 the default `__format__` will use the default `__str__`, which is the standard behavior for Python objects

For those that were relying on, for example,

    class Color(IntEnum):
        RED = 1

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

They will need to add ":d" to ensure the integer output:


This change does work now.
msg392028 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2021-04-27 05:43
New changeset 5987b8c463892e0ab7a63cdae92f34b5eb79732d by Ethan Furman in branch 'master':
bpo-43945: [Enum] Deprecate non-standard mixin format() behavior (GH-25649)
msg395904 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2021-06-16 02:43
Thinking about this some more I am partially reversing course.  The idea behind `IntEnum` and `IntFlag` is to be, as close as possible, drop-in replacements for existing integer-based constants.  If the format() of those two change then they become much less attractive.

So it won't.  Instead, any user-mixed enumerations will get the new behavior:

    class Grades(int, Enum):
        A = 5
        F = 0

will emit a DeprecationWarning now, and in 3.12 `format(Grades.A)` will product 'A' instead of '5'.
msg396083 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2021-06-18 20:15
New changeset f60b07ab6c943fce084772c3c7731ab3bbd213ff by Ethan Furman in branch 'main':
bpo-43945: [Enum] reduce scope of new format() behavior (GH-26752)
msg396097 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2021-06-18 21:25
New changeset 1b4addf3cbd0ef424939681ee67c802328d567ba by Ethan Furman in branch '3.10':
[3.10] bpo-43945: [Enum] reduce scope of new format() behavior (GH-26752)
Date User Action Args
2022-04-11 14:59:44adminsetgithub: 88111
2021-06-29 16:52:05barrysetnosy: + barry
2021-06-18 21:28:01ethan.furmansetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-06-18 21:25:50ethan.furmansetmessages: + msg396097
2021-06-18 20:21:53ethan.furmansetpull_requests: + pull_request25375
2021-06-18 20:15:54ethan.furmansetmessages: + msg396083
2021-06-16 04:31:25ethan.furmansetpull_requests: + pull_request25337
2021-06-16 02:43:07ethan.furmansetmessages: + msg395904
2021-04-27 05:43:08ethan.furmansetmessages: + msg392028
2021-04-27 04:20:15ethan.furmansetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request24340
2021-04-26 23:04:32ethan.furmancreate