classification
Title: Mixin repr overrides Enum repr in some cases
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ethan.furman Nosy List: barry, eli.bendersky, ethan.furman, miss-islington, rmccampbell7, xtreak
Priority: normal Keywords: patch

Created on 2020-02-08 21:01 by rmccampbell7, last changed 2020-09-16 00:01 by ethan.furman. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 22263 merged ethan.furman, 2020-09-15 21:46
PR 22265 merged miss-islington, 2020-09-15 22:58
PR 22266 merged miss-islington, 2020-09-15 22:58
Messages (6)
msg361635 - (view) Author: Ryan McCampbell (rmccampbell7) Date: 2020-02-08 21:01
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.
msg361667 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-02-10 05:55
Bisecting points to issue29577 that introduced this change.
msg376959 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2020-09-15 22:15
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__`.
msg376962 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2020-09-15 22:56
New changeset bff01f3a3aac0c15fe8fbe8b2f561f7927d117a1 by Ethan Furman in branch 'master':
bpo-39587: Enum - use correct mixed-in data type (GH-22263)
https://github.com/python/cpython/commit/bff01f3a3aac0c15fe8fbe8b2f561f7927d117a1
msg376963 - (view) Author: miss-islington (miss-islington) Date: 2020-09-15 23:24
New changeset 8f8ebcca95d3b6ed0a522a9736ab53d6d4f0208c by Miss Islington (bot) in branch '3.8':
bpo-39587: Enum - use correct mixed-in data type (GH-22263)
https://github.com/python/cpython/commit/8f8ebcca95d3b6ed0a522a9736ab53d6d4f0208c
msg376966 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2020-09-15 23:59
New changeset 95b81e2f8c955823dbf5632a817902b8a4916eaa by Miss Islington (bot) in branch '3.9':
bpo-39587: Enum - use correct mixed-in data type (GH-22263) (GH-22266)
https://github.com/python/cpython/commit/95b81e2f8c955823dbf5632a817902b8a4916eaa
History
Date User Action Args
2020-09-16 00:01:58ethan.furmansetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-09-15 23:59:57ethan.furmansetmessages: + msg376966
2020-09-15 23:24:08miss-islingtonsetmessages: + msg376963
2020-09-15 22:58:45miss-islingtonsetpull_requests: + pull_request21322
2020-09-15 22:58:37miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request21321
2020-09-15 22:56:45ethan.furmansetmessages: + msg376962
2020-09-15 22:15:37ethan.furmansetmessages: + msg376959
2020-09-15 21:57:05ethan.furmansetversions: + Python 3.9, Python 3.10, - Python 3.7
2020-09-15 21:46:54ethan.furmansetkeywords: + patch
stage: patch review
pull_requests: + pull_request21319
2020-02-10 05:55:23xtreaksetnosy: + xtreak
messages: + msg361667
2020-02-09 04:17:18rhettingersetassignee: ethan.furman
2020-02-09 04:15:41xtreaksetnosy: + barry, eli.bendersky, ethan.furman
2020-02-08 21:01:31rmccampbell7create