You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Enum.__str__ change could be improved in What's New #98250
#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"
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.
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
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().
Activity
[-]`Enum.__str__` change should be more clear in What's New[/-][+]`Enum.__str__` change could be improved in What's New[/+]ethanfurman commentedon Oct 13, 2022
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, notstr--str()output hasn't changed,format()has changed to matchstr().pythongh-98250: Improve documentation about change to Enum.__format__
pythongh-98250: Describe __str__/__format__ changes more clearly/accu…
CAM-Gerlach commentedon Oct 15, 2022
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.
gh-95913: Copyedit, xref and organize enum section (#98295)
pythongh-95913: Copyedit, xref and organize enum section (pythonGH-98295
gh-95913: Copyedit, xref and organize enum section (GH-98295)
gh-95913: Copyedit, xref and organize enum section (GH-98295)