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

Add IntEnum (like the standard library's IntEnum) #53

Merged
merged 1 commit into from
Jun 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion enumfields/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .enums import Enum
from .enums import Enum, IntEnum
from .fields import EnumField, EnumIntegerField
6 changes: 6 additions & 0 deletions enumfields/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ def __str__(self):
Show our label when Django uses the Enum for displaying in a view
"""
return force_text(self.label)


@python_2_unicode_compatible
class IntEnum(int, Enum):
def __str__(self): # See Enum.__str__
return force_text(self.label)
5 changes: 3 additions & 2 deletions tests/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.db import models
from enum import Enum, IntEnum

from enumfields import EnumField, EnumIntegerField
from enumfields import Enum, IntEnum, EnumField, EnumIntegerField


class MyModel(models.Model):
Expand All @@ -26,6 +25,8 @@ class ZeroEnum(Enum):
class IntegerEnum(IntEnum):
A = 0
B = 1
class Labels:
A = 'foo'

taste = EnumField(Taste, default=Taste.SWEET)
taste_null_default = EnumField(Taste, null=True, blank=True, default=None)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import six
from six import u

from tests.models import MyModel


class Color(Enum):
__order__ = 'RED GREEN BLUE'
Expand Down Expand Up @@ -35,11 +37,13 @@ def test_custom_labels():
# Custom label
assert Color.RED.label == 'Reddish'
assert six.text_type(Color.RED) == 'Reddish'
assert six.text_type(MyModel.IntegerEnum.A) == 'foo'

def test_automatic_labels():
# Automatic label
assert Color.GREEN.label == 'Green'
assert six.text_type(Color.GREEN) == 'Green'
assert six.text_type(MyModel.IntegerEnum.B) == 'B'

def test_lazy_labels():
# Lazy label
Expand Down
7 changes: 4 additions & 3 deletions tests/test_form_fields.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -- encoding: UTF-8 --

from django.db.models import BLANK_CHOICE_DASH
from django.forms.models import modelform_factory
import pytest
from .models import MyModel
Expand All @@ -8,7 +8,7 @@

def get_form(**kwargs):
instance = MyModel(color=MyModel.Color.RED)
FormClass = modelform_factory(MyModel, fields=("color", "zero2"))
FormClass = modelform_factory(MyModel, fields=("color", "zero2", "int_enum"))
return FormClass(instance=instance, **kwargs)


Expand All @@ -26,4 +26,5 @@ def test_bound_form_with_instance():

def test_choices():
form = get_form()
assert form.base_fields["zero2"].choices == [(0, 'ZERO'), (1, 'ONE')]
assert form.base_fields["zero2"].choices == [(0, 'Zero'), (1, 'One')]
assert form.base_fields["int_enum"].choices == BLANK_CHOICE_DASH + [(0, 'foo'), (1, 'B')]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, is this backward incompatible change, since the code for zero2 stays the same but still the choices change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No – the code for zero2 actually does change, albeit in a subtle way :)

IntEnum used to be imported directly from the stdlib.

However, now we're using our fancier IntEnum, which as the same choices behavior as enumfields.Enum, i.e. auto-titling the name for a choice label when no explicit label exists.

Before:

from enum import Enum, IntEnum

After:

from enumfields import Enum, IntEnum, EnumField, EnumIntegerField

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right. I didn't notice that this import change was actually in tests/models.py. Yep. This is all good then.