Skip to content

Enum repr() incorrect when mixed with non-__new__ data types #90266

Description

@ethanfurman
BPO 46108
Nosy @ericvsmith, @ethanfurman
Superseder
  • bpo-40066: Enum: modify repr, str; update docs
  • 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 2022-01-17.16:59:39.171>
    created_at = <Date 2021-12-17.01:13:16.460>
    labels = ['type-bug', '3.8', '3.9', '3.10', '3.11']
    title = 'Enum repr() incorrect when mixed with non-__new__ data types'
    updated_at = <Date 2022-01-17.16:59:39.170>
    user = 'https://github.com/ethanfurman'

    bugs.python.org fields:

    activity = <Date 2022-01-17.16:59:39.170>
    actor = 'ethan.furman'
    assignee = 'ethan.furman'
    closed = True
    closed_date = <Date 2022-01-17.16:59:39.171>
    closer = 'ethan.furman'
    components = []
    creation = <Date 2021-12-17.01:13:16.460>
    creator = 'ethan.furman'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46108
    keywords = []
    message_count = 4.0
    messages = ['408748', '408756', '408775', '410806']
    nosy_count = 2.0
    nosy_names = ['eric.smith', 'ethan.furman']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = '40066'
    type = 'behavior'
    url = 'https://bugs.python.org/issue46108'
    versions = ['Python 3.8', 'Python 3.9', 'Python 3.10', 'Python 3.11']

    Activity

    1. ethanfurman commented on Dec 17, 2021

      @ethanfurman
      MemberAuthor
      from dataclasses import dataclass
      from enum import Enum
      
      @dataclass
      class Foo:
          a: int = 0
      
      class Entries(Foo, Enum):
          ENTRY1 = Foo(1)

      repr(Entries.ENTRY1) != '<Entries.ENTRY1: Foo(a=1)>'

    2. self-assigned this
      on Dec 17, 2021
    3. ericvsmith commented on Dec 17, 2021

      @ericvsmith
      Member

      I know you know this, but here's a version without dataclasses, in case you want to add a test for this, too.

      from enum import Enum
      
      class Foo:
          def __init__(self, a):
              self.a = a
          def __repr__(self):
              return f'Foo(a={self.a!r})'
      
      class Entries(Foo, Enum):
          ENTRY1 = Foo(1)

      repr(Entries.ENTRY1) != '<Entries.ENTRY1: Foo(a=1)>'

    4. ethanfurman commented on Dec 17, 2021

      @ethanfurman
      MemberAuthor

      Thanks, test added. I also updated the title.

      Code updated to treat any __repr__s as affecting the display of the member value instead of the member itself (i.e. the "<...: %r>" portion). if a user actually wants to change the member repr they can assign it when creating the Enum:

          class Entries(Foo, Enum):
              ENTRY = 1
              __repr__ = Foo.__repr__
      assert repr(Entries.ENTRY1) == 'Entries(a=1)'
      
    5. changed the title [-]Enum repr() incorrect when mixed with dataclasses[/-] [+]Enum repr() incorrect when mixed with non-__new__ data types[/+] on Dec 17, 2021
    6. ethanfurman commented on Jan 17, 2022

      @ethanfurman
      MemberAuthor

      Fixed in 3.11.

    7. 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.11only security fixes3.8 (EOL)end of life3.9 (EOL)end of lifetype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions