Skip to content

AttributeError in 3.11 on repr() of enum member if the enum has a non-Enum EnumType parent #102558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vashek opened this issue Mar 9, 2023 · 8 comments
Assignees
Labels
3.11 only security fixes 3.12 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@vashek
Copy link

vashek commented Mar 9, 2023

Bug report

In CPython 3.11.0 - 3.11.2 (but not 3.10.10), I get a weird AttributeError when repr() is called on an Enum member of a class that was defined using multiple inheritance. Minimal code to reproduce:

from enum import Enum, EnumMeta

class _EnumSuperClass(metaclass=EnumMeta):
    pass

class E(_EnumSuperClass, Enum):
    A=1

print(repr(E.A))

With Python up to 3.10, the output is:

<E.A: 1>

With Python 3.11, the output is:

Traceback (most recent call last):
  File "C:\devel\tmp\api\e.py", line 9, in <module>
    print(repr(E.A))
          ^^^^^^^^^
  File "C:\Program Files\Python311\Lib\enum.py", line 1169, in __repr__
    return "<%s.%s: %s>" % (self.__class__.__name__, self._name_, v_repr(self._value_))
                                                                  ^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\enum.py", line 1168, in __repr__
    v_repr = self.__class__._value_repr_ or repr
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: type object 'int' has no attribute '_value_repr_'

Your environment

Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)] on win32
(But reproduced also on 3.11.2)

Linked PRs

@vashek vashek added the type-bug An unexpected behavior, bug, or error label Mar 9, 2023
@AlexWaygood AlexWaygood added stdlib Python modules in the Lib dir 3.11 only security fixes labels Mar 9, 2023
@corona10 corona10 added the 3.12 only security fixes label Mar 9, 2023
@vashek
Copy link
Author

vashek commented Mar 9, 2023

Reproduced also on Linux in case it's important: Python 3.11.2 (main, Mar 1 2023, 14:46:02) [GCC 10.2.1 20210110] on linux

corona10 added a commit to corona10/cpython that referenced this issue Mar 9, 2023
@vashek
Copy link
Author

vashek commented Mar 9, 2023

Wow, this must be the fastest fix I've ever seen implemented in response to a random dude's complaint in the history of paid SLA contracts. Oh wait, I don't have any contract. Kudos and thanks.

@corona10
Copy link
Member

@ethanfurman
I submitted the patch but not sure it is the correct fix(closer to work around)
So please let me know if you have a better fix. (or you can direct fix this)

@ethanfurman
Copy link
Member

Merging in the work-around; will investigate further as time allows.

miss-islington added a commit that referenced this issue Mar 23, 2023
@ethanfurman
Copy link
Member

@vashek What is your actual use-case for that coding style? Making a non-Enum class with EnumMeta as the type is not the intended usage.

@vashek
Copy link
Author

vashek commented Mar 25, 2023

@ethanfurman Now I feel a bit silly, because it seems like I brought it upon myself completely needlessly and wasted your time. Sorry.

What I'm doing is I have a class with some class methods for enums, and then I'm mixing it into the actual enums. I guess when I did that, my thought process was "if I'm going to mix in another class into the enum, and the enum has a special metaclass, surely I can't just use an ordinary class with its ordinary metaclass -- surely the mixed-in classes must have a common metaclass". Now that I googled and read up on the subject, I think I now understand that this would have only been a problem if my base class used a different metaclass. I don't remember whether I had ever tried it without the metaclass -- maybe I haven't. I tried now and it seems to work fine.

I mean the fix is still nice, but I suppose the "bug" was only ever encountered by me and completely unnecessarily anyway, apparently.

@ethanfurman
Copy link
Member

No worries, glad it ended up being an easy fix for us both.

@hauntsaninja
Copy link
Contributor

Thanks, looks like this was fixed and backported!

@ethanfurman ethanfurman changed the title AttributeError in 3.11 on repr() of enum member if the enum has multiple superclasses AttributeError in 3.11 on repr() of enum member if the enum has a non-Enum EnumType parent Mar 27, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 27, 2023
miss-islington added a commit that referenced this issue Mar 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes 3.12 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

5 participants