Skip to content

Mixin repr overrides Enum repr in some cases #83768

Description

@rmccampbell7
BPO 39587
Nosy @warsaw, @ethanfurman, @miss-islington, @tirkarthi
PRs
  • bpo-39587: Enum - use correct mixed-in data type #22263
  • [3.8] bpo-39587: Enum - use correct mixed-in data type (GH-22263) #22265
  • [3.9] bpo-39587: Enum - use correct mixed-in data type (GH-22263) #22266
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/ethanfurman'
    closed_at = <Date 2020-09-16.00:01:58.020>
    created_at = <Date 2020-02-08.21:01:31.642>
    labels = ['3.8', 'type-bug', 'library', '3.9', '3.10']
    title = 'Mixin repr overrides Enum repr in some cases'
    updated_at = <Date 2020-09-16.00:01:58.020>
    user = 'https://bugs.python.org/rmccampbell7'

    bugs.python.org fields:

    activity = <Date 2020-09-16.00:01:58.020>
    actor = 'ethan.furman'
    assignee = 'ethan.furman'
    closed = True
    closed_date = <Date 2020-09-16.00:01:58.020>
    closer = 'ethan.furman'
    components = ['Library (Lib)']
    creation = <Date 2020-02-08.21:01:31.642>
    creator = 'rmccampbell7'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 39587
    keywords = ['patch']
    message_count = 6.0
    messages = ['361635', '361667', '376959', '376962', '376963', '376966']
    nosy_count = 6.0
    nosy_names = ['barry', 'eli.bendersky', 'ethan.furman', 'rmccampbell7', 'miss-islington', 'xtreak']
    pr_nums = ['22263', '22265', '22266']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue39587'
    versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

    Activity

    1. rmccampbell7 commented on Feb 8, 2020

      rmccampbell7mannequin
      MannequinAuthor

      In Python 3.6 the following works:

      class HexInt(int):
          def __repr__(self):
              return hex(self)
      
      class MyEnum(HexInt, enum.Enum):
          A = 1
          B = 2
          C = 3
      >>> MyEnum.A
      <MyEnum.A: 0x1>
      
      However in Python 3.7/8 it instead prints
      >>> MyEnum.A
      0x1

      It uses HexInt's repr instead of Enum's. Looking at the enum.py module it seems that this occurs for mixin classes that don't define __new__ due to a change in the _get_mixins_ method. If I define a __new__ method on the HexInt class then the expected behavior occurs.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Feb 8, 2020
    3. tirkarthi commented on Feb 10, 2020

      @tirkarthi
      Member

      Bisecting points to bpo-29577 that introduced this change.

    4. ethanfurman commented on Sep 15, 2020

      @ethanfurman
      Member

      Yes, the change only considered types with their own copy of __new__ to be actual data types, so in 3.6 HexInt was the recognized data type, but in 3.7+ it was int -- which also meant that HexEnum was considered a simple mix-in and its __repr__ was used instead of Enum.__repr__.

    5. ethanfurman commented on Sep 15, 2020

      @ethanfurman
      Member

      New changeset bff01f3 by Ethan Furman in branch 'master':
      bpo-39587: Enum - use correct mixed-in data type (GH-22263)
      bff01f3

    6. miss-islington commented on Sep 15, 2020

      @miss-islington
      Contributor

      New changeset 8f8ebcc by Miss Islington (bot) in branch '3.8':
      bpo-39587: Enum - use correct mixed-in data type (GH-22263)
      8f8ebcc

    7. ethanfurman commented on Sep 15, 2020

      @ethanfurman
      Member

      New changeset 95b81e2 by Miss Islington (bot) in branch '3.9':
      bpo-39587: Enum - use correct mixed-in data type (GH-22263) (GH-22266)
      95b81e2

    8. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    3.10 (EOL)end of life3.8 (EOL)end of life3.9 (EOL)end of lifestdlibStandard 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