Skip to content

Commit d72d471

Browse files
committed
static analysis fixes, also fix remaining problem for #55 in Django >= 5
1 parent ae9b975 commit d72d471

File tree

4 files changed

+20
-15
lines changed

4 files changed

+20
-15
lines changed

django_enum/choices.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class DjangoSymmetricMixin(SymmetricMixin):
108108
_symmetric_builtins_ = ['name', 'label']
109109

110110

111-
class TextChoices(
111+
class TextChoices( # pylint: disable=too-many-ancestors
112112
DjangoSymmetricMixin,
113113
DjangoTextChoices,
114114
metaclass=DjangoEnumPropertiesMeta
@@ -122,7 +122,7 @@ def __hash__(self):
122122
return DjangoTextChoices.__hash__(self)
123123

124124

125-
class IntegerChoices(
125+
class IntegerChoices( # pylint: disable=too-many-ancestors
126126
DjangoSymmetricMixin,
127127
DjangoIntegerChoices,
128128
metaclass=DjangoEnumPropertiesMeta
@@ -150,6 +150,9 @@ class FloatChoices(
150150
def __hash__(self):
151151
return float.__hash__(self)
152152

153+
def __str__(self):
154+
return str(self.value)
155+
153156

154157
except (ImportError, ModuleNotFoundError):
155158

django_enum/fields.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,17 @@
2727
SmallIntegerField,
2828
)
2929
from django.db.models.query_utils import DeferredAttribute
30-
from django_enum.choices import choices, values
31-
from django_enum.forms import EnumChoiceField, NonStrictSelect
32-
33-
T = TypeVar('T') # pylint: disable=C0103
3430

3531
try:
3632
from django.db.models.expressions import DatabaseDefault
3733
except ImportError: # pragma: no cover
38-
class DatabaseDefault:
39-
pass
34+
class DatabaseDefault: # type: ignore
35+
"""Spoof DatabaseDefault for Django < 5.0"""
36+
37+
from django_enum.choices import choices, values
38+
from django_enum.forms import EnumChoiceField, NonStrictSelect
39+
40+
T = TypeVar('T') # pylint: disable=C0103
4041

4142

4243
def with_typehint(baseclass: Type[T]) -> Type[T]:
@@ -124,7 +125,7 @@ def _try_coerce(
124125
"""
125126
if self.enum is None:
126127
return value
127-
128+
128129
if (self.coerce or force) and not isinstance(value, self.enum):
129130
try:
130131
value = self.enum(value)
@@ -157,7 +158,7 @@ def _try_coerce(
157158
f"'{value}' is not coercible to {self.primitive.__name__} "
158159
f"required by field {self.name}."
159160
) from err
160-
161+
161162
return value
162163

163164
def deconstruct(self) -> Tuple[str, str, List, dict]:

django_enum/tests/settings.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from pathlib import Path
2+
23
from django import VERSION as django_version
34

45
SECRET_KEY = 'psst'
@@ -54,7 +55,7 @@
5455
'django.contrib.admin',
5556
]
5657

57-
if django_version[0:2] >= (5, 0):
58+
if django_version[0:2] >= (5, 0): # pragma: no cover
5859
INSTALLED_APPS.insert(0, 'django_enum.tests.db_default')
5960

6061
try:

django_enum/tests/tests.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44
from time import perf_counter
55

66
from bs4 import BeautifulSoup as Soup
7+
from django import VERSION as django_version
78
from django.core import serializers
89
from django.core.exceptions import ValidationError
910
from django.core.management import call_command
1011
from django.db import connection, transaction
1112
from django.db.models import Q
13+
from django.forms import Form, ModelForm
1214
from django.http import QueryDict
1315
from django.test import Client, LiveServerTestCase, TestCase
1416
from django.urls import reverse
1517
from django.utils.functional import classproperty
1618
from django_enum import TextChoices
1719
from django_enum.choices import choices, labels, names, values
1820
from django_enum.forms import EnumChoiceField # dont remove this
19-
from django.forms import Form, ModelForm
20-
from django import VERSION as django_version
2121
# from django_enum.tests.djenum.enums import (
2222
# BigIntEnum,
2323
# BigPosIntEnum,
@@ -1857,7 +1857,7 @@ class Meta:
18571857

18581858
@property
18591859
def basic_form_class(self):
1860-
from django.core.validators import MinValueValidator, MaxValueValidator
1860+
from django.core.validators import MaxValueValidator, MinValueValidator
18611861

18621862
class BasicForm(Form):
18631863

@@ -3875,7 +3875,7 @@ def test_no_coerce(self):
38753875
pass
38763876

38773877

3878-
if django_version[0:2] >= (5, 0):
3878+
if django_version[0:2] >= (5, 0): # pragma: no cover
38793879
from django_enum.tests.db_default.models import DBDefaultTester
38803880

38813881
class DBDefaultTests(EnumTypeMixin, TestCase):

0 commit comments

Comments
 (0)