Skip to content

Commit 2a2bab5

Browse files
committed
fixup! fixup! fixup! Bump versions and move to GitHub actions (#1)
1 parent 07ca081 commit 2a2bab5

File tree

2 files changed

+17
-35
lines changed

2 files changed

+17
-35
lines changed

django_enumfield/enum.py

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import absolute_import
22

33
import logging
4-
from collections import abc
54
import enum
65
from typing import Any, List, Optional, Sequence, Tuple, TypeVar, Union, cast, Mapping
76
from django.utils.encoding import force_str
@@ -48,12 +47,9 @@ def __get__(self, instance, cls=None):
4847
class Enum(enum.IntEnum):
4948
"""A container for holding and restoring enum values"""
5049

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]]
5753

5854
def __str__(self):
5955
return self.label
@@ -70,23 +66,20 @@ def label(self):
7066
:return: label for value
7167
:rtype: str
7268
"""
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__
7870
return force_str(labels.get(self.value, self.name))
7971

80-
@classproperty
72+
@classproperty # type: ignore[arg-type]
8173
def do_not_call_in_templates(cls):
8274
# type: () -> bool
8375
# Fix for Django templates so that any lookups of enums won't fail
8476
# More info: https://stackoverflow.com/questions/35953132/how-to-access-enum-types-in-django-templates # noqa: E501
8577
return True
8678

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]
9083

9184
def deconstruct(self):
9285
"""
@@ -130,13 +123,8 @@ def default(cls):
130123
IntegerField(choices=my_enum.choices(), default=my_enum.default(), ...
131124
:return Default value, if set.
132125
"""
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__))
140128
return None
141129

142130
@classmethod
@@ -222,14 +210,9 @@ def is_valid_transition(cls, from_value, to_value):
222210
if isinstance(to_value, cls):
223211
to_value = to_value.value
224212

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)
230213
return (
231214
from_value == to_value
232-
or not transitions
215+
or not cls.__transitions__
233216
or (from_value in cls.transition_origins(to_value))
234217
)
235218

@@ -242,9 +225,4 @@ def transition_origins(cls, to_value):
242225
if isinstance(to_value, cls):
243226
to_value = to_value.value
244227

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, [])

mypy.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
[mypy]
2+
pretty = True
3+
4+
show_error_codes = True
25
strict_optional = True
6+
37
plugins =
48
mypy_django_plugin.main
59

0 commit comments

Comments
 (0)