1
- # pylint: disable=C0302
2
1
"""
3
2
Support for Django model fields built from enumeration types.
4
3
"""
@@ -85,7 +84,6 @@ class _DatabaseDefault:
85
84
86
85
PrimitiveT = TypeVar ("PrimitiveT" , bound = Type [SupportedPrimitive ])
87
86
88
-
89
87
condition = "check" if django_version [0 :2 ] < (5 , 1 ) else "condition"
90
88
91
89
@@ -143,7 +141,7 @@ class EnumFieldFactory(type):
143
141
based on their python Enum class types.
144
142
"""
145
143
146
- def __call__ ( # pylint: disable=C0103, R0912, R0911
144
+ def __call__ (
147
145
cls ,
148
146
enum : Optional [Type [Enum ]] = None ,
149
147
primitive : Optional [Type [SupportedPrimitive ]] = None ,
@@ -225,7 +223,7 @@ class EnumTypeChar(TextChoices):
225
223
# make sure all enumeration values are symmetrically coercible to
226
224
# the primitive, if they are not this could cause some strange behavior
227
225
for value in values (enum ):
228
- if value is None or type (value ) is primitive : # pylint: disable=C0123
226
+ if value is None or type (value ) is primitive :
229
227
continue
230
228
try :
231
229
assert type (value )(primitive (value )) == value # type: ignore
@@ -418,7 +416,7 @@ def _coerce_to_value_type(self, value: Any) -> Any:
418
416
and self .primitive
419
417
and not isinstance (value , self .primitive )
420
418
):
421
- return self .primitive (value ) # pylint: disable=E1102
419
+ return self .primitive (value )
422
420
return value
423
421
424
422
def __init__ (
@@ -474,23 +472,21 @@ def _try_coerce(self, value: Any, force: bool = False) -> Union[Enum, Any]:
474
472
475
473
if (self .coerce or force ) and not isinstance (value , self .enum ):
476
474
try :
477
- value = self .enum (value ) # pylint: disable=E1102
475
+ value = self .enum (value )
478
476
except (TypeError , ValueError ):
479
477
try :
480
478
# value = self.primitive(value)
481
479
value = self ._coerce_to_value_type (value )
482
- value = self .enum (value ) # pylint: disable=E1102
480
+ value = self .enum (value )
483
481
except (TypeError , ValueError , DecimalException ):
484
482
try :
485
483
value = self .enum [value ]
486
484
except KeyError as err :
487
485
if len (self ._value_primitives_ ) > 1 :
488
486
for primitive in self ._value_primitives_ :
489
487
try :
490
- return self .enum ( # pylint: disable=E1102
491
- primitive (value )
492
- )
493
- except Exception : # pylint: disable=W0703
488
+ return self .enum (primitive (value ))
489
+ except Exception :
494
490
pass
495
491
value = self ._fallback (value )
496
492
if not isinstance (value , self .enum ) and (
@@ -578,8 +574,8 @@ def get_db_prep_value(self, value, connection, prepared=False) -> Any:
578
574
def from_db_value (
579
575
self ,
580
576
value : Any ,
581
- expression , # pylint: disable=W0613
582
- connection , # pylint: disable=W0613
577
+ expression ,
578
+ connection ,
583
579
) -> Any :
584
580
"""
585
581
Convert the database field value into the Enum type.
@@ -663,7 +659,7 @@ def formfield(self, form_class=None, choices_form_class=None, **kwargs):
663
659
664
660
is_multi = self .enum and issubclass (self .enum , Flag )
665
661
if is_multi and self .enum :
666
- kwargs ["empty_value" ] = self .enum (0 ) # pylint: disable=E1102
662
+ kwargs ["empty_value" ] = self .enum (0 )
667
663
# why fail? - does this fail for single select too?
668
664
# kwargs['show_hidden_initial'] = True
669
665
@@ -694,9 +690,9 @@ def get_choices(
694
690
blank_choice = tuple (BLANK_CHOICE_DASH ),
695
691
limit_choices_to = None ,
696
692
ordering = (),
697
- ): # pylint: disable=W0221
693
+ ):
698
694
if self .enum and issubclass (self .enum , Flag ):
699
- blank_choice = [(self .enum (0 ), "---------" )] # pylint: disable=E1102
695
+ blank_choice = [(self .enum (0 ), "---------" )]
700
696
return [
701
697
(getattr (choice , "value" , choice ), label )
702
698
for choice , label in super ().get_choices (
@@ -722,7 +718,7 @@ def constraint_name(
722
718
:param enum: The enumeration type of the EnumField
723
719
"""
724
720
name = (
725
- f"{ model_class ._meta .app_label } _" # pylint: disable=W0212
721
+ f"{ model_class ._meta .app_label } _"
726
722
f"{ model_class .__name__ } _{ field_name } _"
727
723
f"{ enum .__name__ } "
728
724
)
@@ -732,7 +728,7 @@ def constraint_name(
732
728
733
729
def contribute_to_class (
734
730
self , cls : Type [Model ], name : str , private_only : bool = False
735
- ): # pylint: disable=W0221
731
+ ):
736
732
super ().contribute_to_class (cls , name , private_only = private_only )
737
733
if self .constrained and self .enum and issubclass (self .enum , IntFlag ):
738
734
# It's possible to declare an IntFlag field with negative values -
@@ -751,20 +747,20 @@ def contribute_to_class(
751
747
)
752
748
if self .null :
753
749
constraint |= Q (** {f"{ name } __isnull" : True })
754
- cls ._meta .constraints = [ # pylint: disable=W0212
755
- * cls ._meta .constraints , # pylint: disable=W0212
750
+ cls ._meta .constraints = [
751
+ * cls ._meta .constraints ,
756
752
CheckConstraint (
757
- ** {
753
+ ** { # type: ignore[arg-type]
758
754
condition : constraint ,
759
755
"name" : self .constraint_name (cls , name , self .enum ),
760
756
}
761
757
),
762
- ] # pylint: disable=protected-access
758
+ ]
763
759
# this dictionary is used to serialize the model, so if constraints
764
760
# is not present - they will not be added to migrations
765
- cls ._meta .original_attrs .setdefault ( # pylint: disable=W0212
761
+ cls ._meta .original_attrs .setdefault (
766
762
"constraints" ,
767
- cls ._meta .constraints , # pylint: disable=W0212
763
+ cls ._meta .constraints ,
768
764
)
769
765
770
766
@@ -1173,16 +1169,16 @@ def contribute_to_class(
1173
1169
]
1174
1170
1175
1171
if is_strict or is_conform or (is_eject and self .strict ) and flags :
1176
- constraint = ( # pylint: disable=E1131
1172
+ constraint = (
1177
1173
Q (** {f"{ name } __gte" : min (* flags )})
1178
1174
& Q (** {f"{ name } __lte" : reduce (or_ , flags )})
1179
1175
) | Q (** {name : 0 })
1180
1176
1181
1177
if self .null :
1182
1178
constraint |= Q (** {f"{ name } __isnull" : True })
1183
1179
1184
- cls ._meta .constraints = [ # pylint: disable=W0212
1185
- * cls ._meta .constraints , # pylint: disable=W0212
1180
+ cls ._meta .constraints = [
1181
+ * cls ._meta .constraints ,
1186
1182
CheckConstraint (
1187
1183
** {
1188
1184
condition : constraint ,
@@ -1193,9 +1189,9 @@ def contribute_to_class(
1193
1189
# this dictionary is used to serialize the model, so if
1194
1190
# constraints is not present - they will not be added to
1195
1191
# migrations
1196
- cls ._meta .original_attrs .setdefault ( # pylint: disable=W0212
1192
+ cls ._meta .original_attrs .setdefault (
1197
1193
"constraints" ,
1198
- cls ._meta .constraints , # pylint: disable=W0212
1194
+ cls ._meta .constraints ,
1199
1195
)
1200
1196
if isinstance (self , FlagField ):
1201
1197
# this may have been called by a normal EnumField to bring in flag-like constraints
@@ -1283,8 +1279,8 @@ def get_db_prep_value(self, value: Any, connection, prepared=False):
1283
1279
def from_db_value (
1284
1280
self ,
1285
1281
value : Any ,
1286
- expression , # pylint: disable=W0613
1287
- connection , # pylint: disable=W0613
1282
+ expression ,
1283
+ connection ,
1288
1284
) -> Any :
1289
1285
"""
1290
1286
Convert the database field value into the Enum type.
0 commit comments