The Wayback Machine - https://web.archive.org/web/20221223110339/https://github.com/python/cpython/pull/100387
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

Clarify Enum.__format__() change of mixed-in types in the whatsnew/3.11.rst #100387

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

anze3db
Copy link

@anze3db anze3db commented Dec 21, 2022

Hey! 👋

Please correct me if I'm wrong, but the description of the change Enum.format() change in 3.11.rst doesn't seem accurate. The behavior in older Python versions was to return the member's value and not the member's key as is currently stated. Example code:

from enum import Enum


class Foo(str, Enum):
    BAR = "baz"


print("{}".format(Foo.BAR))


class Foo(int, Enum):
    BAR = 1


print("{}".format(Foo.BAR))

Running this on Python 3.10 prints:

bar
1

And not

BAR
BAR

as the changelog states:

Changed Enum.format() (the default for format(), str.format() and f-strings) of enums with mixed-in types (e.g. int, str) to also include the class name in the output, not just the member’s key. This matches the existing behavior of enum.Enum.str(), returning e.g. 'AnEnum.MEMBER' for an enum AnEnum(str, Enum) instead of just 'MEMBER'.

I noticed this because we were using the str mixin with all Enum classes as a hack to get to the value without needing to write Foo.BAR.value 🫣 The new behavior in Python 3.11 broke our code and the current phrasing in the changelog makes it feel this was just a minor bugfix. Should this change be a bit more prominent since it can be a breaking change?

By the way, thank you for adding StrEnum, it's exactly what we needed!

PS: Not sure if this needs a GitHub issue number, it's not exactly a typo, but it's still a minor change. I'm happy to open an issue if needed. Also let me know if I need to open the PR against any other branches as well. 🙇

@bedevere-bot bedevere-bot added awaiting review docs Documentation in the Doc dir skip news labels Dec 21, 2022
@cpython-cla-bot
Copy link

cpython-cla-bot bot commented Dec 21, 2022

All commit authors signed the Contributor License Agreement.
CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review docs Documentation in the Doc dir skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants