classification
Title: Enum sets _member_type_ to instantiated values but not the class
Type: behavior Stage: resolved
Components: Versions: Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ethan.furman Nosy List: barry, eli.bendersky, ethan.furman, lambacck, python-dev
Priority: normal Keywords: patch

Created on 2013-08-02 19:34 by lambacck, last changed 2013-08-04 15:42 by python-dev. This issue is now closed.

Files
File name Uploaded Description Edit
enum_member_type_on_class.patch lambacck, 2013-08-02 19:34 review
Messages (8)
msg194199 - (view) Author: Chris Lambacher (lambacck) * Date: 2013-08-02 19:34
It would be useful to set the discovered member_type to the Enum class and not just the instance. Attached is a patch to add _member_type_ to the enum_class.
msg194206 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2013-08-02 20:13
Ah, so the Enum class has the mixin class as wall as / instead of the Enum member (which should find it via normal attribute lookup).

I have no problem with that.  I'll need to make a couple more changes to the code, add a test, etc., etc.

It won't make the first alpha, but should be ready for the second.
msg194221 - (view) Author: Eli Bendersky (eli.bendersky) * (Python committer) Date: 2013-08-02 23:45
Can you clarify why it would be useful? Note that we're talking about private non-documented members here.
msg194225 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2013-08-03 00:21
I also admit to being curious as to the reason it is useful, especially since it is, at this point, an implementation detail.

Even so, it still makes more sense to have that attribute on the class instead of the instance.
msg194254 - (view) Author: Chris Lambacher (lambacck) * Date: 2013-08-03 12:57
My use case is a generic mixin for Enums and a generic mixin for Django ORM fields that uses the Enums to generate choices. 

The Enum mixin has to call cls.__class__._get_mixins_(cls.__bases__) to get the member_type so that it can call the member_type.__new__ method (currently using this for int and str). I'm currently setting _member_type_ on the class if it doesn't already exist in the Enum class.

The Django ORM field mixin has a to_python method where it is supposed to take input from the db/web and turn it into the python type (in this case an Enum). If we get a str from the web and we are going to an int, we need to run int on it. The generic way to do this in the mixin is to pass the value to _member_type_ as a function.

I think I have all the bugs out of my implementation so I should be able to extract it out of my app and make it open source this week.
msg194300 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2013-08-03 20:50
Well, aside from not having a clue as to what Chris is trying to do, should we make _member_type_ public?  The only reason I put it there was to aid introspection -- Enum does not use it.
msg194305 - (view) Author: Eli Bendersky (eli.bendersky) * (Python committer) Date: 2013-08-03 21:48
-1 on making more internals public.
msg194384 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-08-04 15:42
New changeset b034418e840b by Ethan Furman in branch 'default':
Close #18635: Move class level private attribute from instance to class.
http://hg.python.org/cpython/rev/b034418e840b
History
Date User Action Args
2013-08-04 15:42:42python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg194384

resolution: fixed
stage: resolved
2013-08-03 21:48:31eli.benderskysetmessages: + msg194305
2013-08-03 20:50:42ethan.furmansetmessages: + msg194300
2013-08-03 12:57:32lambaccksetmessages: + msg194254
2013-08-03 00:21:17ethan.furmansetmessages: + msg194225
2013-08-02 23:45:54eli.benderskysetmessages: + msg194221
2013-08-02 20:13:50ethan.furmansetnosy: + barry, eli.bendersky
messages: + msg194206
2013-08-02 20:06:02ethan.furmansetassignee: ethan.furman

nosy: + ethan.furman
2013-08-02 19:34:16lambacckcreate