Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enum] standardize format() behavior #88111

Closed
ethanfurman opened this issue Apr 26, 2021 · 5 comments
Closed

[Enum] standardize format() behavior #88111

ethanfurman opened this issue Apr 26, 2021 · 5 comments
Assignees
Labels
3.10 only security fixes 3.11 only security fixes type-bug An unexpected behavior, bug, or error

Comments

@ethanfurman
Copy link
Member

BPO 43945
Nosy @warsaw, @ethanfurman
PRs
  • bpo-43945: [Enum] Deprecate non-standard mixin format() behavior #25649
  • bpo-43945: [Enum] reduce scope of new format() behavior #26752
  • [3.10] bpo-43945: [Enum] reduce scope of new format() behavior (GH-26752) #26791
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/ethanfurman'
    closed_at = <Date 2021-06-18.21:28:01.096>
    created_at = <Date 2021-04-26.23:04:32.335>
    labels = ['type-bug', '3.10', '3.11']
    title = '[Enum] standardize format() behavior'
    updated_at = <Date 2021-06-29.16:52:05.269>
    user = 'https://github.com/ethanfurman'

    bugs.python.org fields:

    activity = <Date 2021-06-29.16:52:05.269>
    actor = 'barry'
    assignee = 'ethan.furman'
    closed = True
    closed_date = <Date 2021-06-18.21:28:01.096>
    closer = 'ethan.furman'
    components = []
    creation = <Date 2021-04-26.23:04:32.335>
    creator = 'ethan.furman'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 43945
    keywords = ['patch']
    message_count = 5.0
    messages = ['391995', '392028', '395904', '396083', '396097']
    nosy_count = 2.0
    nosy_names = ['barry', 'ethan.furman']
    pr_nums = ['25649', '26752', '26791']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue43945'
    versions = ['Python 3.10', 'Python 3.11']

    @ethanfurman
    Copy link
    Member Author

    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.

    @ethanfurman ethanfurman added 3.10 only security fixes 3.11 only security fixes labels Apr 26, 2021
    @ethanfurman ethanfurman self-assigned this Apr 26, 2021
    @ethanfurman ethanfurman added the type-bug An unexpected behavior, bug, or error label Apr 26, 2021
    @ethanfurman
    Copy link
    Member Author

    New changeset 5987b8c by Ethan Furman in branch 'master':
    bpo-43945: [Enum] Deprecate non-standard mixin format() behavior (GH-25649)
    5987b8c

    @ethanfurman
    Copy link
    Member Author

    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'.

    @ethanfurman
    Copy link
    Member Author

    New changeset f60b07a by Ethan Furman in branch 'main':
    bpo-43945: [Enum] reduce scope of new format() behavior (GH-26752)
    f60b07a

    @ethanfurman
    Copy link
    Member Author

    New changeset 1b4addf by Ethan Furman in branch '3.10':
    [3.10] bpo-43945: [Enum] reduce scope of new format() behavior (GH-26752)
    1b4addf

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes 3.11 only security fixes type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant