Skip to content

Commit b855066

Browse files
committed
remove top level imports
1 parent 9cd9ae1 commit b855066

File tree

26 files changed

+501
-533
lines changed

26 files changed

+501
-533
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,16 @@ Many packages aim to ease usage of Python enumerations as model fields. Most wer
173173
assert TextChoicesExample.objects.filter(color='FF0000').first() == instance
174174
```
175175

176-
While they should be unnecessary if you need to integrate with code that expects an interface fully compatible with Django's ``TextChoices`` and ``IntegerChoices`` django-enum provides ``TextChoices`` and ``IntegerChoices`` types that derive from enum-properties and Django's enum types. So the above enumeration could also be written:
176+
While they should be unnecessary if you need to integrate with code that expects an interface fully compatible with Django's ``TextChoices`` and ``IntegerChoices`` django-enum provides ``TextChoices``, ``IntegerChoices``, ``FlagChoices`` and ``FloatChoices`` types that derive from enum-properties and Django's ``Choices``. So the above enumeration could also be written:
177177

178178
```python
179179

180180
from django_enum.choices import TextChoices
181181

182182
class Color(TextChoices):
183183

184+
# label is added as a symmetric property by the base class
185+
184186
rgb: Annotated[t.Tuple[int, int, int], Symmetric()]
185187
hex: Annotated[str, Symmetric(case_fold=True)]
186188

django_enum/__init__.py

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,9 @@
99
*******************************************************************************
1010
"""
1111

12-
from django_enum.choices import (
13-
DjangoEnumPropertiesMeta,
14-
FlagChoices,
15-
FloatChoices,
16-
IntegerChoices,
17-
TextChoices,
18-
)
19-
from django_enum.converters import register_enum_converter
20-
from django_enum.fields import EnumField, FlagField
21-
from django_enum.filters import EnumFilter, FilterSet
22-
from django_enum.forms import (
23-
EnumChoiceField,
24-
EnumFlagField,
25-
NonStrictSelect,
26-
NonStrictSelectMultiple,
27-
)
12+
from django_enum.fields import EnumField
2813

29-
__all__ = [
30-
"EnumField",
31-
"FlagField",
32-
"TextChoices",
33-
"IntegerChoices",
34-
"FlagChoices",
35-
"FloatChoices",
36-
"DjangoEnumPropertiesMeta",
37-
"EnumChoiceField",
38-
"EnumFlagField",
39-
"FilterSet",
40-
"EnumFilter",
41-
"NonStrictSelect",
42-
"NonStrictSelectMultiple",
43-
"register_enum_converter",
44-
]
14+
__all__ = ["EnumField"]
4515

4616
VERSION = (2, 0, 0)
4717

django_enum/choices.py

Lines changed: 90 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
from django_enum.utils import choices, names
1616

17+
from enum_properties import DecomposeMixin, EnumPropertiesMeta, SymmetricMixin
18+
1719
ChoicesType = (
1820
model_enums.ChoicesType
1921
if django_version[0:2] >= (5, 0)
@@ -23,139 +25,97 @@
2325
DEFAULT_BOUNDARY = getattr(enum, "KEEP", None)
2426

2527

26-
try:
27-
from enum_properties import DecomposeMixin, EnumPropertiesMeta, SymmetricMixin
28-
29-
class DjangoEnumPropertiesMeta(EnumPropertiesMeta, ChoicesType): # type: ignore
30-
"""
31-
A composite meta class that combines Django's Choices metaclass with
32-
enum-properties metaclass. This metaclass will add Django's expected
33-
choices attribute and label properties to enumerations and
34-
enum-properties' generic property support.
35-
"""
36-
37-
@property
38-
def names(self):
39-
"""
40-
For some eccentric enums list(Enum) is empty, so we override names
41-
if empty
42-
"""
43-
return super().names or names(self, override=True)
44-
45-
@property
46-
def choices(self):
47-
"""
48-
For some eccentric enums list(Enum) is empty, so we override
49-
choices if empty
50-
"""
51-
return super().choices or choices(self, override=True)
52-
53-
class DjangoSymmetricMixin(SymmetricMixin):
54-
"""
55-
An enumeration mixin that makes Django's Choices type label field
56-
symmetric.
57-
"""
58-
59-
_symmetric_builtins_ = ["name", "label"]
60-
61-
class TextChoices(
62-
DjangoSymmetricMixin, DjangoTextChoices, metaclass=DjangoEnumPropertiesMeta
63-
):
64-
"""
65-
A character enumeration type that extends Django's TextChoices and
66-
accepts enum-properties property lists.
67-
"""
68-
69-
def __hash__(self):
70-
return DjangoTextChoices.__hash__(self)
71-
72-
label: str
73-
74-
class IntegerChoices(
75-
DjangoSymmetricMixin, DjangoIntegerChoices, metaclass=DjangoEnumPropertiesMeta
76-
):
77-
"""
78-
An integer enumeration type that extends Django's IntegerChoices and
79-
accepts enum-properties property lists.
80-
"""
81-
82-
def __hash__(self):
83-
return DjangoIntegerChoices.__hash__(self)
84-
85-
label: str
86-
87-
class FloatChoices(
88-
DjangoSymmetricMixin, float, Choices, metaclass=DjangoEnumPropertiesMeta
89-
):
90-
"""
91-
A floating point enumeration type that accepts enum-properties
92-
property lists.
93-
"""
28+
class DjangoEnumPropertiesMeta(EnumPropertiesMeta, ChoicesType): # type: ignore
29+
"""
30+
A composite meta class that combines Django's Choices metaclass with
31+
enum-properties metaclass. This metaclass will add Django's expected
32+
choices attribute and label properties to enumerations and
33+
enum-properties' generic property support.
34+
"""
9435

95-
def __hash__(self):
96-
return float.__hash__(self)
97-
98-
def __str__(self):
99-
return str(self.value)
100-
101-
label: str
102-
103-
# mult inheritance type hint bug
104-
class FlagChoices( # type: ignore
105-
DecomposeMixin,
106-
DjangoSymmetricMixin,
107-
enum.IntFlag,
108-
Choices,
109-
metaclass=DjangoEnumPropertiesMeta,
110-
# default boundary argument gets lost in the inheritance when choices
111-
# is included if it is not explicitly specified
112-
**({"boundary": DEFAULT_BOUNDARY} if DEFAULT_BOUNDARY is not None else {}),
113-
):
36+
@property
37+
def names(self):
11438
"""
115-
An integer flag enumeration type that accepts enum-properties property
116-
lists.
39+
For some eccentric enums list(Enum) is empty, so we override names
40+
if empty
11741
"""
42+
return super().names or names(self, override=True)
11843

119-
def __hash__(self):
120-
return enum.IntFlag.__hash__(self)
121-
122-
label: str
123-
124-
except (ImportError, ModuleNotFoundError):
125-
# 3.11 - extend from Enum so base type check does not throw type error
126-
class MissingEnumProperties(enum.Enum):
127-
"""Throw error if choice types are used without enum-properties"""
128-
129-
def __init__(self, *args, **kwargs):
130-
raise ImportError(
131-
f"{self.__class__.__name__} requires enum-properties to be "
132-
f"installed."
133-
)
134-
135-
DjangoSymmetricMixin = MissingEnumProperties # type: ignore
136-
137-
class DjangoEnumPropertiesMeta(ChoicesType): # type: ignore
44+
@property
45+
def choices(self):
13846
"""
139-
Throw error if metaclass is used without enum-properties
140-
141-
Needs to be strict subclass of same metaclass as Enum to make it to
142-
the ImportError.
47+
For some eccentric enums list(Enum) is empty, so we override
48+
choices if empty
14349
"""
144-
145-
def __init__(cls, *args, **kwargs):
146-
raise ImportError(
147-
f"{cls.__class__.__name__} requires enum-properties to be "
148-
f"installed."
149-
)
150-
151-
class TextChoices(DjangoSymmetricMixin, str, Choices): # type: ignore
152-
"""Raises ImportError on class definition"""
153-
154-
class IntegerChoices(DjangoSymmetricMixin, int, Choices): # type: ignore
155-
"""Raises ImportError on class definition"""
156-
157-
class FloatChoices(DjangoSymmetricMixin, float, Choices): # type: ignore
158-
"""Raises ImportError on class definition"""
159-
160-
class FlagChoices(DjangoSymmetricMixin, enum.IntFlag, Choices): # type: ignore
161-
"""Raises ImportError on class definition"""
50+
return super().choices or choices(self, override=True)
51+
52+
class DjangoSymmetricMixin(SymmetricMixin):
53+
"""
54+
An enumeration mixin that makes Django's Choices type label field
55+
symmetric.
56+
"""
57+
58+
_symmetric_builtins_ = ["name", "label"]
59+
60+
class TextChoices(
61+
DjangoSymmetricMixin, DjangoTextChoices, metaclass=DjangoEnumPropertiesMeta
62+
):
63+
"""
64+
A character enumeration type that extends Django's TextChoices and
65+
accepts enum-properties property lists.
66+
"""
67+
68+
def __hash__(self):
69+
return DjangoTextChoices.__hash__(self)
70+
71+
label: str
72+
73+
class IntegerChoices(
74+
DjangoSymmetricMixin, DjangoIntegerChoices, metaclass=DjangoEnumPropertiesMeta
75+
):
76+
"""
77+
An integer enumeration type that extends Django's IntegerChoices and
78+
accepts enum-properties property lists.
79+
"""
80+
81+
def __hash__(self):
82+
return DjangoIntegerChoices.__hash__(self)
83+
84+
label: str
85+
86+
class FloatChoices(
87+
DjangoSymmetricMixin, float, Choices, metaclass=DjangoEnumPropertiesMeta
88+
):
89+
"""
90+
A floating point enumeration type that accepts enum-properties
91+
property lists.
92+
"""
93+
94+
def __hash__(self):
95+
return float.__hash__(self)
96+
97+
def __str__(self):
98+
return str(self.value)
99+
100+
label: str
101+
102+
# mult inheritance type hint bug
103+
class FlagChoices( # type: ignore
104+
DecomposeMixin,
105+
DjangoSymmetricMixin,
106+
enum.IntFlag,
107+
Choices,
108+
metaclass=DjangoEnumPropertiesMeta,
109+
# default boundary argument gets lost in the inheritance when choices
110+
# is included if it is not explicitly specified
111+
**({"boundary": DEFAULT_BOUNDARY} if DEFAULT_BOUNDARY is not None else {}),
112+
):
113+
"""
114+
An integer flag enumeration type that accepts enum-properties property
115+
lists.
116+
"""
117+
118+
def __hash__(self):
119+
return enum.IntFlag.__hash__(self)
120+
121+
label: str

0 commit comments

Comments
 (0)