[Python-Dev] PEP 435 - requesting pronouncement (original) (raw)

Eli Bendersky eliben at gmail.com
Sun May 5 02:49:56 CEST 2013


On Sat, May 4, 2013 at 4:27 PM, Tim Delaney <timothy.c.delaney at gmail.com>wrote:

Typo line 171: <Colro.blue: 3>

Fixed, thanks.

One thing I'd like to be clear in the PEP about is whether enumtype and EnumDict.enumnames should be documented, or whether they're considered implementation details.

No, they should not. Not only are they implementation details, they are details of the reference implementation, not the actual stdlib module. The reference implementation will naturally serve as a basis for the stdlib module, but it still has to undergo a review in which implementation details can change. Note that usually we do not document implementation details of stdlib modules, but this doesn't prevent some people from using them if they really want to.

I'd like to make a subclass of Enum that accepts ... for auto-valued enums but that requires subclassing the metaclass and access to classdict.enumnames. I can get to enumtype via type(Enum), but EnumDict.enumnames requires knowing the attribute. It would sufficient for my purposes if it was just documented that the passed classdict had a enumnames attribute.

In testing the below, I've also discovered a bug in the reference implementation - currently it will not handle an mro like: Thanks! Tim - did you sign the contributor CLA for Python? Since the reference implementation is aimed for becoming the stdlib enum eventually, we'd probably need you to sign that before we can accept patches from you.

Eli

(<Enum 'AutoIntEnum'>, <Enum 'IntEnum'>, <class 'int'>, <Enum 'Enum'>, <class 'object'>)

Apply the following patch to make that work: diff -r 758d43b9f732 ref435.py --- a/ref435.py Fri May 03 18:59:32 2013 -0700 +++ b/ref435.py Sun May 05 09:23:25 2013 +1000 @@ -116,7 +116,11 @@ if bases[-1] is Enum: objtype = bases[0] else: - objtype = bases[-1].mro[1] # e.g. (IntEnum, int, Enum, object) + for base in bases[-1].mro: + if not issubclass(base, Enum): + objtype = base + break + else: objtype = object # save enum items into separate mapping so they don't get baked into My auto-enum implementation (using the above patch - without it you can get the essentially the same results with class AutoIntEnum(int, Enum, metaclass=autoenum). class autoenum(type(Enum)): def new(metacls, cls, bases, classdict): temp = type(classdict)() names = set(classdict.enumnames) i = 0 for k in classdict.enumnames: v = classdict[k] if v is Ellipsis: v = i else: i = v i += 1 temp[k] = v for k, v in classdict.items(): if k not in names: temp[k] = v return super(autoenum, metacls).new(metacls, cls, bases, temp) class AutoNumberedEnum(Enum, metaclass=autoenum): pass class AutoIntEnum(IntEnum, metaclass=autoenum): pass class TestAutoNumber(AutoNumberedEnum): a = ... b = 3 c = ... class TestAutoInt(AutoIntEnum): a = ... b = 3 c = ... print(TestAutoNumber, list(TestAutoNumber)) print(TestAutoInt, list(TestAutoInt)) ---------- Run ---------- <Enum 'TestAutoNumber'> [<TestAutoNumber.a: 0>, <TestAutoNumber.b: 3>, <TestAutoNumber.c: 4>] <Enum 'TestAutoInt'> [<TestAutoInt.a: 0>, <TestAutoInt.b: 3>, <TestAutoInt.c: 4>] Tim Delaney -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20130504/437fd3f7/attachment.html>



More information about the Python-Dev mailing list