You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Enum: mixin classes don't mix well with already mixed Enums #73763
The Color class works fine, but the IntColor fails. This is due to the way the base data type and __new__ method is discovered. If we switch the order of the bases
classIntColor(IntFlag, AllEnum)
it works, but having to put the mixin class last is both bizarre and unworkable when the mixin should be overriding behavior.
only allow one base Enum class
(fails with All and IntFlag)
only allow base Enum classes that are compatible
(so not an Enum and a Flag)
(would require multiple base classes for the same behavior: DocEnum,
DocFlag, etc.)
only allow one mixin
(before/after that one mixin the inheritance is linear)
(same issue as above)
only allow one non-Enum mixin, critical Enum is last one in the bases,
previous Enums in bases are mixins
(all sunder/dunder methods come from critical Enum, all normal methods
and attributes are in normal base order)
be smarter about determining/saving the correct __new__ and base data type
The last one needs to happen, and some form of the next-to-last one (hopefully allowing for more than one non-Enum mixin).
New changeset 0c076ca by Ethan Furman in branch '3.7':
[3.7] bpo-29577: Enum: mixin classes don't mix well with already mixed Enums (GH-9328) (GH-9486) 0c076ca
Activity
ethanfurman commentedon Feb 16, 2017
Consider:
--------
The Color class works fine, but the IntColor fails. This is due to the way the base data type and __new__ method is discovered. If we switch the order of the bases
it works, but having to put the mixin class last is both bizarre and unworkable when the mixin should be overriding behavior.
ethanfurman commentedon Feb 16, 2017
To get the above code snippet to fail properly:
ethanfurman commentedon Feb 16, 2017
While only IntColor fails with an error, Color also fails as it is not a Flag type Enum:
>>> list(Color) [<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]It should have values of 1, 2, 4.
ethanfurman commentedon Feb 16, 2017
Possibilities:
only allow one base Enum class
(fails with All and IntFlag)
only allow base Enum classes that are compatible
(so not an Enum and a Flag)
(would require multiple base classes for the same behavior: DocEnum,
DocFlag, etc.)
only allow one mixin
(before/after that one mixin the inheritance is linear)
(same issue as above)
only allow one non-Enum mixin, critical Enum is last one in the bases,
previous Enums in bases are mixins
(all sunder/dunder methods come from critical Enum, all normal methods
and attributes are in normal base order)
be smarter about determining/saving the correct __new__ and base data type
The last one needs to happen, and some form of the next-to-last one (hopefully allowing for more than one non-Enum mixin).
ethanfurman commentedon Sep 22, 2018
New changeset 5bdab64 by Ethan Furman in branch 'master':
bpo-29577: Enum: mixin classes don't mix well with already mixed Enums (GH-9328)
5bdab64
ethanfurman commentedon Sep 22, 2018
New changeset 0c076ca by Ethan Furman in branch '3.7':
[3.7] bpo-29577: Enum: mixin classes don't mix well with already mixed Enums (GH-9328) (GH-9486)
0c076ca