Author ncoghlan
Recipients ncoghlan
Date 2013-05-11.13:55:34
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1368280534.51.0.41696456409.issue17954@psf.upfronthosting.co.za>
In-reply-to
Content
Guido chose to follow Java in enforcing the invariant "Enum members are instances of that Enum" for PEP 435 (i.e. "assert (all(isinstance(x, SomeEnum) for x in SomeEnum)"). As a consequence, the Enum metaclass prevents subclassing of Enums with defined members.

This is a reasonable design choice, but one that limits the applicability of the standard library enum solution for use cases that currently rely on this feature of a custom enum implementation (including flufl.enum, the original inspiration for this feature).

An alternative reasonable design choice is to allow extension of enumerations (similar to flufl.enum) and relax the invariant to "Enum members are an instance of that Enum or an Enum-derived parent class of that Enum" (i.e. "assert (all(issubclass(type(x), Enum) and type(x) in SomeEnum.mro() for x in SomeEnum)")

There is no need to support this directly in the standard library, but it would be valuable to make it straightforward to support in an Enum variant by subclassing the standard metaclass (similar to the customisation mechanisms provided to support things like autonumbered enums through a derived metaclass). Currently, implementing this behaviour involves overriding a private method of the metaclass (EnumMetaclass._get_mixins)
History
Date User Action Args
2013-05-11 13:55:34ncoghlansetrecipients: + ncoghlan
2013-05-11 13:55:34ncoghlansetmessageid: <1368280534.51.0.41696456409.issue17954@psf.upfronthosting.co.za>
2013-05-11 13:55:34ncoghlanlinkissue17954 messages
2013-05-11 13:55:34ncoghlancreate