1
1
from __future__ import absolute_import
2
2
3
3
import logging
4
- from collections import abc
5
4
import enum
6
5
from typing import Any , List , Optional , Sequence , Tuple , TypeVar , Union , cast , Mapping
7
6
from django .utils .encoding import force_str
@@ -48,12 +47,9 @@ def __get__(self, instance, cls=None):
48
47
class Enum (enum .IntEnum ):
49
48
"""A container for holding and restoring enum values"""
50
49
51
- # TODO: Can be uncommented once https://github.com/python/mypy/issues/12132
52
- # has been resolved. If we keep these uncommented we'd pollute with mypy errors
53
- # everywhere someone declares these attributes on their own enum class.
54
- # __labels__ = {} # type: Dict[int, str]
55
- # __default__ = None # type: Optional[int]
56
- # __transitions__ = {} # type: Dict[int, Sequence[int]]
50
+ __labels__ = {} # type: Mapping[int, str]
51
+ __default__ = None # type: Optional[int]
52
+ __transitions__ = {} # type: Mapping[int, Sequence[int]]
57
53
58
54
def __str__ (self ):
59
55
return self .label
@@ -70,23 +66,20 @@ def label(self):
70
66
:return: label for value
71
67
:rtype: str
72
68
"""
73
- # TODO: Can be uncommented once https://github.com/python/mypy/issues/12132
74
- # has been resolved.
75
- # labels = self.__class__.__labels__
76
- labels : Mapping [int , str ] = getattr (self .__class__ , "__labels__" , {})
77
- assert isinstance (labels , abc .Mapping )
69
+ labels = self .__class__ .__labels__
78
70
return force_str (labels .get (self .value , self .name ))
79
71
80
- @classproperty
72
+ @classproperty # type: ignore[arg-type]
81
73
def do_not_call_in_templates (cls ):
82
74
# type: () -> bool
83
75
# Fix for Django templates so that any lookups of enums won't fail
84
76
# More info: https://stackoverflow.com/questions/35953132/how-to-access-enum-types-in-django-templates # noqa: E501
85
77
return True
86
78
87
- @classproperty
88
- def values (cls ): # type: ignore
89
- return {member .value : member for member in cls }
79
+ @classproperty # type: ignore[arg-type]
80
+ def values (cls ):
81
+ # type: () -> Mapping[int, Enum]
82
+ return {member .value : member for member in cls } # type: ignore[attr-defined]
90
83
91
84
def deconstruct (self ):
92
85
"""
@@ -130,13 +123,8 @@ def default(cls):
130
123
IntegerField(choices=my_enum.choices(), default=my_enum.default(), ...
131
124
:return Default value, if set.
132
125
"""
133
- # TODO: Can be uncommented once https://github.com/python/mypy/issues/12132
134
- # has been resolved.
135
- # value = cls.__default__
136
- value : Optional [int ] = getattr (cls , "__default__" , None )
137
- assert value is None or isinstance (value , int )
138
- if value is not None :
139
- return cast (Enum , cls (value ))
126
+ if cls .__default__ is not None :
127
+ return cast (Enum , cls (cls .__default__ ))
140
128
return None
141
129
142
130
@classmethod
@@ -222,14 +210,9 @@ def is_valid_transition(cls, from_value, to_value):
222
210
if isinstance (to_value , cls ):
223
211
to_value = to_value .value
224
212
225
- # TODO: Can be uncommented once https://github.com/python/mypy/issues/12132
226
- # has been resolved.
227
- # transitions = cls.__transitions__
228
- transitions : Mapping [int , Sequence [int ]] = getattr (cls , "__transitions__" , {})
229
- assert isinstance (transitions , abc .Mapping )
230
213
return (
231
214
from_value == to_value
232
- or not transitions
215
+ or not cls . __transitions__
233
216
or (from_value in cls .transition_origins (to_value ))
234
217
)
235
218
@@ -242,9 +225,4 @@ def transition_origins(cls, to_value):
242
225
if isinstance (to_value , cls ):
243
226
to_value = to_value .value
244
227
245
- # TODO: Can be uncommented once https://github.com/python/mypy/issues/12132
246
- # has been resolved.
247
- # transitions = cls.__transitions__
248
- transitions : Mapping [int , Sequence [int ]] = getattr (cls , "__transitions__" , {})
249
- assert isinstance (transitions , abc .Mapping )
250
- return transitions .get (to_value , [])
228
+ return cls .__transitions__ .get (to_value , [])
0 commit comments