-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow subclassing of Enums #29
base: master
Are you sure you want to change the base?
Conversation
Hi @jessamynsmith, thanks for your contribution! We are looking into merging #26 so that EnumField uses the native enum class. Could you give it a spin to see if it solves subclassing in your case as well? I believe we will merge your PR in the 1.x branch, and the new enum in 2.x/master branch. |
Hm, it's not possible to subclass enum in Python 3, so maybe we should not allow it in Python 2 to avoid confusion if someone decides to upgrade 2->3 at some point. |
Hello, I checked out #26 and the tests only run on python 3.4. The project on which I'm using the forked version of django-enumfield is a Python 3.4.3 project, and my changeset passes tests in 2.x and 3.x, so I'm not sure what you mean by subclassing won't work. Do you mean once you are using the enum34 package? |
@jessamynsmith yeah, exactly, native enum does not support subclassing so we probably should disallow it in all branches Python 3.4.3 (default, Apr 27 2015, 10:32:50)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.49)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import enum
>>> class A(enum.Enum): X = 1
...
>>> class B(A): Y = 2
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/andrei/.pyenv/versions/3.4.3/lib/python3.4/enum.py", line 93, in __new__
member_type, first_enum = metacls._get_mixins_(bases)
File "/Users/andrei/.pyenv/versions/3.4.3/lib/python3.4/enum.py", line 361, in _get_mixins_
raise TypeError("Cannot extend enumerations")
TypeError: Cannot extend enumerations |
What is the recommendation if you need two enumfields which share most of the members? |
@jessamynsmith I see your point that it would be nice to have an easy set to subclass enums. Since the native enum does not allow it, we may try doing something with the metaclass, however, my gut feeling tells me that this is wrong. You could achieve similar effect by applying a some sort of clone-function to build a new enum from an existing one if you like it. I am not convinced that we should support subclassing until the native enum does it, so you would normally have to duplicate the code. |
I'll continue using my fork for now, and see if I can come up with a solid solution that doesn't involve code duplication. Thank you for the conversation, it gave me good food for thought! |
The current released version of enumfield does not allow subclassing in a way that the parent enum values are inherited. With this change, you can declare an enum with a particular set of values, and then declare a child enum that extends it with additional values.