django-enumfield icon indicating copy to clipboard operation
django-enumfield copied to clipboard

Allow subclassing of Enums

Open jessamynsmith opened this issue 9 years ago • 7 comments

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.

jessamynsmith avatar Nov 16 '15 04:11 jessamynsmith

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.

andreif avatar Nov 16 '15 09:11 andreif

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.

andreif avatar Nov 16 '15 12:11 andreif

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 avatar Nov 16 '15 13:11 jessamynsmith

@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

andreif avatar Nov 16 '15 13:11 andreif

What is the recommendation if you need two enumfields which share most of the members?

jessamynsmith avatar Nov 16 '15 13:11 jessamynsmith

@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.

andreif avatar Nov 16 '15 17:11 andreif

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!

jessamynsmith avatar Nov 16 '15 17:11 jessamynsmith