Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.

Commit

Permalink
Fix failure due to Python3.8 Enum behavior
Browse files Browse the repository at this point in the history
- Update tox, to cover Django versions `3.0.*` and `3.1.*`
- Limit DRF version for Django 1 support
- Exclude some random folders from `flake8`
  • Loading branch information
RadoRado committed Dec 17, 2020
1 parent f913ad8 commit c3683c0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
24 changes: 18 additions & 6 deletions django_enum_choices/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,24 @@ def validate(self, value, *args, **kwargs):
if not self.blank and value in self.empty_values:
raise ValidationError(self.error_messages['blank'], code='blank')

if value not in self.enum_class:
raise ValidationError(
self.error_messages['invalid_choice'],
code='invalid_choice',
params={'value': value}
)
enum_value = value

if not isinstance(enum_value, self.enum_class):
try:
enum_value = self.to_enum_value(value)

if enum_value not in self.enum_class:
raise ValidationError(
self.error_messages['invalid_choice'],
code='invalid_choice',
params={'value': value}
)
except ValidationError:
raise ValidationError(
self.error_messages['invalid_choice'],
code='invalid_choice',
params={'value': value}
)

def value_to_string(self, obj):
value = self.value_from_object(obj)
Expand Down
2 changes: 1 addition & 1 deletion django_enum_choices/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ def prepare_value(self, value):
return value

def valid_value(self, value):
return value in self.enum_class
return isinstance(value, self.enum_class) and value in self.enum_class
3 changes: 1 addition & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ commit = True

[flake8]
max-line-length = 120
exclude = .tox,.git,*/migrations/*,*/static/CACHE/*,docs,node_modules,*/settings/*,.venv,*/e2e/e2e/*

exclude = .tox,.git,*/migrations/*,*/static/CACHE/*,docs,node_modules,*/settings/*,.venv,*/e2e/*
31 changes: 24 additions & 7 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
[tox]
envlist =
lint-py{37}
django22-py{37,36}
django21-py{37,36,35}
django111-py{37,36,35}
lint-py{37,38}
django31-py{38,37,36}
django30-py{38,37,36}
django22-py{38,37,36}
django21-py{38,37,36,35}
django111-py{38,37,36,35}

[testenv]
deps =
{[base]deps}
django31: {[django]3.1}
django30: {[django]3.0}
django22: {[django]2.2}
django21: {[django]2.1}
django111: {[django]1.11}
Expand All @@ -20,6 +24,11 @@ deps =
flake8
commands = flake8 django_enum_choices/

[testenv:lint-py38]
deps =
flake8
commands = flake8 django_enum_choices/

[base]
deps =
pytest
Expand All @@ -29,8 +38,16 @@ deps =
psycopg2

[django]
3.1 =
Django>=3.1.0,<3.2.0
djangorestframework>=3.7.3
django-filter>=2.2.0
3.0 =
Django>=3.0.0,<3.1.0
djangorestframework>=3.7.3
django-filter>=2.2.0
2.2 =
Django>=2.2.0,<2.3.0
Django>=2.2.0,<2.2.17
djangorestframework>=3.7.3
django-filter>=2.2.0
2.1 =
Expand All @@ -39,5 +56,5 @@ deps =
django-filter>=2.2.0
1.11 =
Django>=1.11.0,<2.0.0
djangorestframework>=3.6.2
django-filter>=2.2.0
djangorestframework>=3.6.2,<3.9.0
django-filter>=2.2.0

0 comments on commit c3683c0

Please sign in to comment.