Visitar URL original
Add note about IntEnum gotcha by offby1 · Pull Request #19334 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20260311094008/https://github.com/python/cpython/pull/19334
Skip to content
/ cpython Public

Add note about IntEnum gotcha#19334

Closed
offby1 wants to merge 1 commit intopython:mainfrom
offby1:master
Closed

Add note about IntEnum gotcha#19334
offby1 wants to merge 1 commit intopython:mainfrom
offby1:master

Conversation

@offby1
Copy link
Contributor

@offby1 offby1 commented Apr 3, 2020

I discovered that "{}".format(s) on an IntEnum returned a different string than str(s).
This behavior surprised me (and at least one other person on the Python discord); I figured it'd be good to draw attention to it.

@offby1 offby1 requested a review from ethanfurman as a code owner April 3, 2020 15:19
@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

Recognized GitHub username

We couldn't find a bugs.python.org (b.p.o) account corresponding to the following GitHub usernames:

@offby1

This might be simply due to a missing "GitHub Name" entry in one's b.p.o account settings. This is necessary for legal reasons before we can look at this contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

[0, 1]


Note that the :class:`IntEnum` class is defined by mixing in :class:`int` to :class:`Enum`, which has some surprising
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This violates our style by describing an intentional design feature as surprising or dangerous. Instead, the devguide states that docs be written affirmatively to show what the tool actually does and how to access its features.

For example, this would be an improvement:

Enumeration support several different display styles:

    >>> repr(Color.red)
    '<Color.red: 1>'
    >>> str(Color.red)
    'Color.red'
    >>> format(Color.red)
   '1'

These three styles can be accessed in f-string as follows:

    print(f'{Color.red!r} or {Color.red!s} or {Color.red}' )

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@ethanfurman
Copy link
Member

My apologies for missing this! As of 3.11 the behavior of str has changed for enums with a mixed in type: str uses the mixed in type instead of Enum.__str__ so that str and format have the same output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting changes docs Documentation in the Doc dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants