Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jessamynsmith
Copy link
Contributor

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.

@andreif
Copy link
Contributor

andreif commented Nov 16, 2015

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
Copy link
Contributor

andreif commented Nov 16, 2015

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.

@jessamynsmith
Copy link
Contributor Author

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?

@andreif
Copy link
Contributor

andreif commented Nov 16, 2015

@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

@jessamynsmith
Copy link
Contributor Author

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

@andreif
Copy link
Contributor

andreif commented Nov 16, 2015

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

@jessamynsmith
Copy link
Contributor Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants