◐ Shell
reader mode source ↗
Skip to content

bpo-40066: Enum: modify repr() and str()#22392

Merged
ethanfurman merged 21 commits into
python:masterfrom
ethanfurman:enum-global_repr
Mar 31, 2021
Merged

bpo-40066: Enum: modify repr() and str()#22392
ethanfurman merged 21 commits into
python:masterfrom
ethanfurman:enum-global_repr

Conversation

@ethanfurman

@ethanfurman ethanfurman commented Sep 24, 2020

Copy link
Copy Markdown
Member

Enumerations created from Enum._convert_ will have their repr() changed to be

module.member_name

and their str() to be

member_name

The exception is StrEnum, whose str() will be

value

as in, the member's value.

https://bugs.python.org/issue40066

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hide comment

Nice. How did you track down all the docs that need to be changed? (I note there aren't any doc changes do to the change in str().)

@ethanfurman

Copy link
Copy Markdown
Member Author

Nice. How did you track down all the docs that need to be changed? (I note there aren't any doc changes do to the change in str().)

grep -R "<[A-Za-z\.]*: [^>]*>" *

Assuming no one comes up with a strong reason to not commit this PR, I will need to do some rewriting of enum.rst -- there's currently a good-sized section dealing with valueless Enums and updating the repr() to not show the value, etc.

@gvanrossum

Copy link
Copy Markdown
Member

grep -R "<[A-Za-z\.]*: [^>]*>" *

That doesn't look for occurrences of the str() though, which doesn't have any funky markup.

Assuming no one comes up with a strong reason to not commit this PR, I will need to do some rewriting of enum.rst -- there's currently a good-sized section dealing with valueless Enums and updating the repr() to not show the value, etc.

Are you planning to do that in the same PR? (I recommend it.)

FWIW you're unlikely to get anyone to tell you not to commit this from GitHub reviewers or bpo readers. Was there a python-ideas thread? Maybe you want to ask Raymond (if he didn't already concur)?

@gvanrossum

Copy link
Copy Markdown
Member

Who is this PR waiting for now?

@ethanfurman

Copy link
Copy Markdown
Member Author

GvR wrote:

Who is this PR waiting for now?

For me to make a few changes, and hopefully for Raymond to chime in. I'll update the bpo issue in case he didn't see my email.

non-StrEnum `__str__` will print the member name
StrEnum `__str__` will print the member value
create enum._stdlib_enum decorator for Enums created the normal way: it
adjusts `__repr__` and `__str__` to be the same as if the Enum had been
created using `_convert_`

update various modules to use the new decorator
if `enum.__str__` is either `Enum.__str__` or `global_enum_str` use the
value's `__format__`
repr() = class.member_name
str() = member_name
rename to `global_enum` and have `global_enum` update the module's
namespace with the members

remove `_stdlib_enum` where the enums were not being exported to the
module's namespace
31 hidden items Load more…
* rename global_int_repr --> global_enum_repr
* adjust global_flag_repr to print hex number for out-of-range values
* extract function when _generate_next_value_ is a static method
- repr() is now ``enum_class.member_name``
- str() is now ``member_name``
- add HOW-TO section for ``Enum``
- change main documentation to be an API reference
@ethanfurman

Copy link
Copy Markdown
Member Author

@gvanrossum RE: looking for Enum.__str__ output in docs: I manually checked the module documentation for each one that had enum constants.

@ethanfurman ethanfurman changed the title bpo-40066: Enum global repr() and str() Mar 31, 2021
@ethanfurman ethanfurman merged commit b775106 into python:master Mar 31, 2021
@ethanfurman ethanfurman deleted the enum-global_repr branch April 15, 2021 14:23
tacaswell added a commit to tacaswell/matplotlib that referenced this pull request May 4, 2021
In Python 3.10 the repr and str representation of Enums changed from

 str: 'ClassName.NAME' -> 'NAME'
 repr: '<ClassName.NAME: value>' -> 'ClassName.NAME'

which is more consistent with what str/repr should do, however this breaks
boilerplate which needs to get the ClassName.NAME version in all versions of
Python. Thus, we locally monkey patch our preferred str representation in
here.

bpo-40066
python/cpython#22392
tacaswell added a commit to tacaswell/matplotlib that referenced this pull request May 4, 2021
In Python 3.10 the repr and str representation of Enums changed from

 str: 'ClassName.NAME' -> 'NAME'
 repr: '<ClassName.NAME: value>' -> 'ClassName.NAME'

which is more consistent with what str/repr should do, however this breaks
boilerplate which needs to get the ClassName.NAME version in all versions of
Python. Thus, we locally monkey patch our preferred str representation in
here.

bpo-40066
python/cpython#22392
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants