GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `Enum.__str__` change could be improved in What's New · Issue #98250 · python/cpython

Enum.__str__ change could be improved in What's New #98250

New issue
Copy link
New issue
Copy link
Closed
#98295
Closed
Enum.__str__ change could be improved in What's New#98250
#98295
Copy link
Assignees
ethanfurmanCAM-Gerlach
Labels
3.11only security fixesonly security fixesdocsDocumentation in the Doc dirDocumentation in the Doc dirstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory

Description

@mdboom
mdboom
opened on Oct 13, 2022
Issue body actions

#30582 (in 3.11) introduced a change to Enum.__str__ so that it shows the enum name and the member name, whereas prior it only included the member name.

This could be a breaking change in some cases. As an example in real world code, the Azure CLI uses enums for the acceptable values of command-line flags, and then uses str on the enum to generate the string that is passed to the subprocess as arguments.

For example, this enum is used to define a "compute model":

class ComputeModelType(str, Enum):

    provisioned = "Provisioned"
    serverless = "Serverless"

And then it's used here to construct arguments for a subprocess:

self.cmd('sql db update -g {} --server {} --name {} --compute-model {}'
                 .format(resource_group, server, database_name, compute_model_serverless),

The changelog entry for this change just says:

IntEnum / IntFlag / StrEnum fixes: these now inherit from ReprEnum so the str() output now matches format() output, which is the data types’ (so both str(AnIntEnum.ONE) and format(AnIntEnum.ONE) is equal to '1').

This is unlikely to be easily findable for users running into this change, since StrEnum doesn't exist in Python 3.10, and it's a little obscure. I would suggest:

The default str() of an Enum has changed to include both Enum name and the member name (Enum.MEMBER), whereas before it was just the member name. To continue to use the old behavior, inherit from IntEnum or StrEnum.

Does this make sense? Are there other cases I'm missing? Happy to file a PR, but thought I would get some discussion first.

Reacted by Jelle Zijlstra

Activity

  1. mdboom
    added
    docsDocumentation in the Doc dirDocumentation in the Doc dir
    on Oct 13, 2022
  2. mdboom
    changed the title [-]`Enum.__str__` change should be more clear in What's New[/-] [+]`Enum.__str__` change could be improved in What's New[/+] on Oct 13, 2022
  3. mdboom
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    3.11only security fixesonly security fixes
    on Oct 13, 2022
  4. AlexWaygood
    assigned
    ethanfurman
    on Oct 13, 2022
  5. ethanfurman commented on Oct 13, 2022

    @ethanfurman
    ethanfurman
    on Oct 13, 2022
    Member
    More actions

    I'm more than happy to have help crafting a better What's New entry. Keep in mind that the change you are talking about is format, not str -- str() output hasn't changed, format() has changed to match str().

  6. mdboom
    added a commit that references this issue on Oct 14, 2022

    pythongh-98250: Improve documentation about change to Enum.__format__

    3cd6e55
  7. CAM-Gerlach
    added a commit that references this issue on Oct 15, 2022

    pythongh-98250: Describe __str__/__format__ changes more clearly/accu…

    a596720
  8. CAM-Gerlach commented on Oct 15, 2022

    @CAM-Gerlach
    CAM-Gerlach
    on Oct 15, 2022
    Member
    More actions

    Hey, I didn't see this before I first submitted #98295, but I'd noticed something similar, and I've revised that PR to further resolve this issue.

  9. ezio-melotti
    closed this as completedin #98295on Oct 18, 2022
  10. ezio-melotti
    added a commit that references this issue on Oct 18, 2022

    gh-95913: Copyedit, xref and organize enum section (#98295)

    73e5180
  11. miss-islington
    added 2 commits that reference this issue on Oct 18, 2022

    pythongh-95913: Copyedit, xref and organize enum section (pythonGH-98295

    7f8ca17

    gh-95913: Copyedit, xref and organize enum section (GH-98295)

    d798b59
  12. CAM-Gerlach
    self-assigned this
    on Oct 18, 2022
  13. pablogsal
    added a commit that references this issue on Oct 22, 2022

    gh-95913: Copyedit, xref and organize enum section (GH-98295)

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

Metadata

Metadata

Assignees

  • @CAM-Gerlach
    CAM-Gerlach
  • @ethanfurman
    ethanfurman

Labels

3.11only security fixesonly security fixesdocsDocumentation in the Doc dirDocumentation in the Doc dirstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.