Skip to content

Commit e908d23

Browse files
committed
support enum-properties >=2.0
1 parent 64a5331 commit e908d23

File tree

3 files changed

+61
-31
lines changed

3 files changed

+61
-31
lines changed

django_enum/choices.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class TextChoices(
6868

6969
def __hash__(self):
7070
return DjangoTextChoices.__hash__(self)
71+
72+
label: str
7173

7274
class IntegerChoices(
7375
DjangoSymmetricMixin, DjangoIntegerChoices, metaclass=DjangoEnumPropertiesMeta
@@ -79,6 +81,8 @@ class IntegerChoices(
7981

8082
def __hash__(self):
8183
return DjangoIntegerChoices.__hash__(self)
84+
85+
label: str
8286

8387
class FloatChoices(
8488
DjangoSymmetricMixin, float, Choices, metaclass=DjangoEnumPropertiesMeta
@@ -94,6 +98,8 @@ def __hash__(self):
9498
def __str__(self):
9599
return str(self.value)
96100

101+
label: str
102+
97103
# mult inheritance type hint bug
98104
class FlagChoices( # type: ignore
99105
DecomposeMixin,
@@ -112,6 +118,8 @@ class FlagChoices( # type: ignore
112118

113119
def __hash__(self):
114120
return enum.IntFlag.__hash__(self)
121+
122+
label: str
115123

116124
except (ImportError, ModuleNotFoundError):
117125
# 3.11 - extend from Enum so base type check does not throw type error

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ packages = [
4646
[tool.poetry.dependencies]
4747
python = ">=3.8,<4.0"
4848
Django = ">=3.2,<6.0"
49-
enum-properties = {version = "^1.7.0", optional = true}
49+
enum-properties = {version = ">=2.0.0", optional = true}
5050
django-filter = {version = ">=21", optional = true}
5151
djangorestframework = {version = "^3.9", optional = true}
5252

@@ -76,6 +76,7 @@ numpy = [
7676
django-stubs = {extras = ["compatible-mypy"], version = ">=4.2.7"}
7777
furo = "^2024.8.6"
7878
ruff = "^0.6.3"
79+
typing-extensions = "^4.12.2"
7980

8081
[tool.poetry.group.psycopg2]
8182
optional = true

tests/enum_prop/enums.py

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
from datetime import date, datetime, time, timedelta
2-
from decimal import Decimal, DecimalException
3-
2+
from decimal import Decimal
3+
import typing as t
4+
from typing_extensions import Annotated
45
from django.db.models import IntegerChoices as DjangoIntegerChoices
56
from django.db.models import TextChoices as DjangoTextChoices
67
from django.utils.translation import gettext as _
7-
from enum_properties import (
8-
EnumProperties,
9-
IntEnumProperties,
10-
IntFlagProperties,
11-
p,
12-
s,
13-
)
8+
from enum_properties import EnumProperties, IntEnumProperties, Symmetric, s
149

1510
from django_enum import FlagChoices, FloatChoices, IntegerChoices, TextChoices
1611
from tests.utils import try_convert
@@ -28,7 +23,11 @@ class DJTextEnum(DjangoTextChoices):
2823
C = "C", "Label C"
2924

3025

31-
class TextEnum(TextChoices, p("version"), p("help"), s("aliases", case_fold=True)):
26+
class TextEnum(TextChoices):
27+
version: int
28+
help: str
29+
aliases: Annotated[t.List[str], Symmetric(case_fold=True)]
30+
3231
VALUE1 = (
3332
"V1",
3433
"Value1",
@@ -59,7 +58,9 @@ class TextEnum(TextChoices, p("version"), p("help"), s("aliases", case_fold=True
5958
)
6059

6160

62-
class Constants(FloatChoices, s("symbol")):
61+
class Constants(FloatChoices):
62+
symbol: Annotated[str, Symmetric()]
63+
6364
PI = 3.14159265358979323846264338327950288, "Pi", "π"
6465
e = 2.71828, "Euler's Number", "e"
6566
GOLDEN_RATIO = 1.61803398874989484820458683436563811, "Golden Ratio", "φ"
@@ -101,7 +102,10 @@ class BigPosIntEnum(IntegerChoices):
101102
VAL3 = 2147483648, "Value 2147483648"
102103

103104

104-
class BigIntEnum(IntegerChoices, s("pos"), p("help")):
105+
class BigIntEnum(IntegerChoices):
106+
pos: Annotated[BigPosIntEnum, Symmetric()]
107+
help: str
108+
105109
VAL0 = (
106110
-2147483649,
107111
"Value -2147483649",
@@ -118,7 +122,9 @@ class BigIntEnum(IntegerChoices, s("pos"), p("help")):
118122
)
119123

120124

121-
class DateEnum(EnumProperties, s("label")):
125+
class DateEnum(EnumProperties):
126+
label: Annotated[str, Symmetric()]
127+
122128
BRIAN = date(1984, 8, 7), "Brian"
123129
EMMA = date(1989, 7, 27), "Emma"
124130
HUGO = date(2016, 9, 9), "Hugo"
@@ -141,7 +147,9 @@ def __hash__(self):
141147
return super().__hash__()
142148

143149

144-
class DateTimeEnum(EnumProperties, s("label")):
150+
class DateTimeEnum(EnumProperties):
151+
label: Annotated[str, Symmetric()]
152+
145153
ST_HELENS = datetime(1980, 5, 18, 8, 32, 0), "Mount St. Helens"
146154
PINATUBO = datetime(1991, 6, 15, 20, 9, 0), "Pinatubo"
147155
KATRINA = datetime(2005, 8, 29, 5, 10, 0), "Katrina"
@@ -164,7 +172,9 @@ def __hash__(self):
164172
return super().__hash__()
165173

166174

167-
class TimeEnum(EnumProperties, s("label")):
175+
class TimeEnum(EnumProperties):
176+
label: Annotated[str, Symmetric()]
177+
168178
COB = time(17, 0, 0), "Close of Business"
169179
LUNCH = time(12, 30, 0), "Lunch"
170180
MORNING = time(9, 0, 0), "Morning"
@@ -187,7 +197,9 @@ def __hash__(self):
187197
return super().__hash__()
188198

189199

190-
class DurationEnum(EnumProperties, s("label", case_fold=True)):
200+
class DurationEnum(EnumProperties):
201+
label: Annotated[str, Symmetric(case_fold=True)]
202+
191203
DAY = timedelta(days=1), "DAY"
192204
WEEK = timedelta(weeks=1), "WEEK"
193205
FORTNIGHT = timedelta(weeks=2), "FORTNIGHT"
@@ -210,7 +222,9 @@ def __hash__(self):
210222
return super().__hash__()
211223

212224

213-
class DecimalEnum(EnumProperties, s("label", case_fold=True)):
225+
class DecimalEnum(EnumProperties):
226+
label: Annotated[str, Symmetric(case_fold=True)]
227+
214228
ONE = Decimal("0.99"), "One"
215229
TWO = Decimal("0.999"), "Two"
216230
THREE = Decimal("0.9999"), "Three"
@@ -237,20 +251,21 @@ def __hash__(self):
237251
return super().__hash__()
238252

239253

240-
class PrecedenceTest(
241-
s("prop1"),
242-
s("prop2"),
243-
IntegerChoices,
244-
s("prop3", case_fold=False),
245-
s("prop4", case_fold=True),
246-
):
254+
class PrecedenceTest(IntegerChoices):
255+
prop1: Annotated[t.Union[int, str], Symmetric()]
256+
prop2: Annotated[float, Symmetric()]
257+
prop3: Annotated[str, Symmetric(case_fold=False)]
258+
prop4: Annotated[t.List[t.Union[str, float, int]], Symmetric(case_fold=True)]
259+
247260
P1 = 0, "Precedence 1", 3, 0.1, _("First"), ["0.4", "Fourth", 1]
248-
P2 = 1, "Precedence 2", 2, 0.2, _("Second"), {"0.3", "Third", 2}
261+
P2 = 1, "Precedence 2", 2, 0.2, _("Second"), ["0.3", "Third", 2]
249262
P3 = 2, "Precedence 3", "1", 0.3, _("Third"), [0.2, "Second", 3]
250-
P4 = 3, "Precedence 4", 0, 0.4, _("Fourth"), {0.1, "First", 4}
263+
P4 = 3, "Precedence 4", 0, 0.4, _("Fourth"), [0.1, "First", 4]
251264

252265

253-
class CarrierFrequency(FlagChoices, p("mhz")):
266+
class CarrierFrequency(FlagChoices):
267+
mhz: float
268+
254269
L1 = 1, 1575.420
255270
L2 = 2, 1227.600
256271
L5 = 4, 1176.450
@@ -269,8 +284,12 @@ class CarrierFrequency(FlagChoices, p("mhz")):
269284
B3 = 4096, 1268.520
270285

271286

272-
class GNSSConstellation(FlagChoices, s("country"), p("satellites"), p("frequencies")):
273-
_symmetric_builtins_ = [s("label", case_fold=True)]
287+
class GNSSConstellation(FlagChoices):
288+
_symmetric_builtins_ = ["name", s("label", Symmetric(case_fold=True))]
289+
290+
country: Annotated[str, Symmetric()]
291+
satellites: int
292+
frequencies: CarrierFrequency
274293

275294
GPS = (
276295
1,
@@ -313,12 +332,14 @@ class LargeNegativeField(IntegerChoices):
313332
ZERO = -1, "ZERO"
314333

315334

316-
class ExternEnum(IntEnumProperties, s("label", case_fold=True)):
335+
class ExternEnum(IntEnumProperties):
317336
"""
318337
Tests that externally defined (i.e. not deriving from choices enums
319338
are supported.
320339
"""
321340

341+
label: Annotated[str, Symmetric(case_fold=True)]
342+
322343
ONE = 1, "One"
323344
TWO = 2, "Two"
324345
THREE = 3, "Three"

0 commit comments

Comments
 (0)