Skip to content

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

Description

@vashek

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

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Mar 9, 2023
  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    3.11only security fixes
    on Mar 9, 2023
  3. vashek commented on Mar 9, 2023

    @vashek
    Author

    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

  4. added a commit that references this issue on Mar 9, 2023
  5. vashek commented on Mar 9, 2023

    @vashek
    Author

    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.

  6. added 2 commits that reference this issue on Mar 11, 2023
  7. corona10 commented on Mar 11, 2023

    @corona10
    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)

  8. added a commit that references this issue on Mar 23, 2023
  9. added a commit that references this issue on Mar 23, 2023
  10. ethanfurman commented on Mar 23, 2023

    @ethanfurman
    Member

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

  11. added a commit that references this issue on Mar 23, 2023
  12. ethanfurman commented on Mar 25, 2023

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

  13. vashek commented on Mar 25, 2023

    @vashek
    Author

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

  14. ethanfurman commented on Mar 25, 2023

    @ethanfurman
    Member

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

  15. hauntsaninja commented on Mar 25, 2023

    @hauntsaninja
    Contributor

    Thanks, looks like this was fixed and backported!

  16. added a commit that references this issue on Mar 27, 2023
  17. 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[/+] on Mar 27, 2023
  18. added a commit that references this issue on Mar 27, 2023
  19. added 2 commits that reference this issue on Mar 27, 2023
  20. added 2 commits that reference this issue on Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.11only security fixes3.12only security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions