Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link
@mdboom

Description

@mdboom
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.

JelleZijlstra

Metadata

Metadata

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

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.