This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: add classattribute to enum to handle non-Enum attributes
Type: enhancement Stage: resolved
Components: Versions: Python 3.6
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: ethan.furman Nosy List: barry, eli.bendersky, ethan.furman
Priority: normal Keywords:

Created on 2016-02-02 16:27 by ethan.furman, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg259399 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2016-02-02 16:27
The rules for what objects in an Enum become members and which do not are fairly straight-forward:

__double_underscore__ do not (but is reserved for Python)
_single_underscore_ do not (but is reserved for Enum itself)
any descriptored object (such as functions) do not

Which means the proper way to add constants/attributes to an Enum is to write a descriptor, but most folks don't think about that when the Enum is not working properly they (okay, and me :/ ) just add the double-underscore.

This question has already come up a couple times on StackOverflow:
- http://stackoverflow.com/q/17911188/208880
- http://stackoverflow.com/q/34465739/208880

While this doesn't come up very often, that just means it is even more likely to have the attribute be __double_underscored__ instead of descriptored.

The solution is have a descriptor in the Enum module for this case.  While it would be possible to have several (constant-unless-mutable, constant-even-if-mutable, not-constant, possibly others) I think the not-constant would be sufficient (aka a writable property), although I am not opposed to a constant-unless mutable version as well.

The not-constant version would look like this (I'll attach patch later):

class classattribute:
    def __init__(self, value):
        self.value = value
    def __get__(self, *args):
        return self.value
    def __set__(self, value):
        self.value = value
    def __repr__(self):
        return '%s(%r)' % (self.__class__.__name__, self.value)
msg266985 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2016-06-02 23:20
One possible downside to the `classattribute` route is that we have a descriptor whose only purpose is to shield the item from becoming a member; the up-side is that it's simple to implement.

Another possibility is `skip`:

class skip:
    """
    Protects item from becaming an Enum member during class creation.
    """
    def __init__(self, value):
        self.value = value

    def __get__(self, instance, ownerclass=None):
        return self.value

The advantage is that it is replaced by the metaclass with the stored value, so we have no extraneous descriptor after the Enum is created; the downside is that it requires change in the metaclass to do the right thing.

Thoughts?
msg273189 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2016-08-20 07:03
We'll keep the stdlib version simple.
History
Date User Action Args
2022-04-11 14:58:27adminsetgithub: 70454
2016-08-20 07:03:00ethan.furmansetstatus: open -> closed
resolution: rejected
messages: + msg273189

stage: needs patch -> resolved
2016-06-02 23:20:18ethan.furmansettype: behavior -> enhancement
messages: + msg266985
stage: needs patch
2016-02-02 16:27:31ethan.furmancreate