Created on 2017-02-16 02:09 by ethan.furman, last changed 2017-02-16 12:29 by ethan.furman.
|msg287910 - (view)||Author: Ethan Furman (ethan.furman) *||Date: 2017-02-16 02:09|
Consider: -------- class AllEnum(Enum): @classattr def ALL(cls): members = list(cls) all_value = None if members: all_value = members for member in members[1:]: all_value |= member cls.ALL = all_value return all_value class Color(AllEnum, Flag): RED = auto() BLUE = auto() GREEN = auto() class IntColor(AllEnum, IntFlag): RED = auto() BLUE = auto() GREEN = auto() -------- 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 class IntColor(IntFlag, AllEnum) it works, but having to put the mixin class last is both bizarre and unworkable when the mixin should be overriding behavior.
|msg287914 - (view)||Author: Ethan Furman (ethan.furman) *||Date: 2017-02-16 05:13|
To get the above code snippet to fail properly: - remove the @classattr line - prepend from enum import Enum, Flag, IntFlag, auto
|msg287931 - (view)||Author: Ethan Furman (ethan.furman) *||Date: 2017-02-16 11:45|
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.
|msg287934 - (view)||Author: Ethan Furman (ethan.furman) *||Date: 2017-02-16 12:29|
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).
|2017-02-16 12:29:34||ethan.furman||set||messages: + msg287934|
|2017-02-16 11:45:48||ethan.furman||set||messages: + msg287931|
|2017-02-16 05:13:06||ethan.furman||set||messages: + msg287914|