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

Created on 2021-04-26 23:04 by ethan.furman, last changed 2021-04-27 05:43 by ethan.furman.

Pull Requests
URL Status Linked Edit
PR 25649 merged ethan.furman, 2021-04-27 04:20
Messages (2)
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:

    f'{Color.RED:d}'

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)
https://github.com/python/cpython/commit/5987b8c463892e0ab7a63cdae92f34b5eb79732d
History
Date User Action Args
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