Skip to content
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

Mixin repr overrides Enum repr in some cases #83768

Closed
rmccampbell7 mannequin opened this issue Feb 8, 2020 · 6 comments
Closed

Mixin repr overrides Enum repr in some cases #83768

rmccampbell7 mannequin opened this issue Feb 8, 2020 · 6 comments
Assignees
Labels
3.8 (EOL) end of life 3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@rmccampbell7
Copy link
Mannequin

rmccampbell7 mannequin commented Feb 8, 2020

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']

    @rmccampbell7
    Copy link
    Mannequin Author

    rmccampbell7 mannequin commented Feb 8, 2020

    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.

    @rmccampbell7 rmccampbell7 mannequin added 3.7 (EOL) end of life 3.8 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Feb 8, 2020
    @tirkarthi
    Copy link
    Member

    Bisecting points to bpo-29577 that introduced this change.

    @ethanfurman ethanfurman added 3.9 only security fixes 3.10 only security fixes and removed 3.7 (EOL) end of life labels Sep 15, 2020
    @ethanfurman
    Copy link
    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__.

    @ethanfurman
    Copy link
    Member

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

    @miss-islington
    Copy link
    Contributor

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

    @ethanfurman
    Copy link
    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

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 (EOL) end of life 3.9 only security fixes 3.10 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

    3 participants