|
14 | 14 |
|
15 | 15 | from django_enum.utils import choices, names
|
16 | 16 |
|
| 17 | +from enum_properties import DecomposeMixin, EnumPropertiesMeta, SymmetricMixin |
| 18 | + |
17 | 19 | ChoicesType = (
|
18 | 20 | model_enums.ChoicesType
|
19 | 21 | if django_version[0:2] >= (5, 0)
|
|
23 | 25 | DEFAULT_BOUNDARY = getattr(enum, "KEEP", None)
|
24 | 26 |
|
25 | 27 |
|
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 | + """ |
94 | 35 |
|
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): |
114 | 38 | """
|
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 |
117 | 41 | """
|
| 42 | + return super().names or names(self, override=True) |
118 | 43 |
|
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): |
138 | 46 | """
|
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 |
143 | 49 | """
|
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