Skip to content

Commit 6d6807d

Browse files
authored
Restore support for mypy v1.13 (#2669)
Currently mypy v1.13 is the minimum supported version, but the fix made to the choices plugin code in #2646 made use of `TypeInfo.enum_members` which was introduced in v1.14. This commit adds a backport of the version introduced in v1.14 which can be removed when `django-stubs` bumps its minimum version.
1 parent e566001 commit 6d6807d

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

mypy_django_plugin/transformers/choices.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from mypy.nodes import MemberExpr, NameExpr, SuperExpr, TypeAlias, TypeInfo
1+
from mypy.nodes import MemberExpr, NameExpr, SuperExpr, TypeAlias, TypeInfo, Var
22
from mypy.plugin import AttributeContext
33
from mypy.typeanal import make_optional_type
44
from mypy.types import (
@@ -18,6 +18,25 @@
1818
from mypy_django_plugin.lib import fullnames, helpers
1919

2020

21+
# TODO: [mypy 1.14+] Remove this backport of `TypeInfo.enum_members`.
22+
def _get_enum_members(info: TypeInfo) -> list[str]:
23+
try:
24+
return info.enum_members
25+
except AttributeError: # mypy < 1.14
26+
pass
27+
28+
return [
29+
name
30+
for name, sym in info.names.items()
31+
if (
32+
isinstance(sym.node, Var)
33+
and name not in ("_ignore_", "_order_", "__order__")
34+
and not name.startswith("__")
35+
and sym.node.has_explicit_value
36+
)
37+
]
38+
39+
2140
def _has_lazy_label(node: TypeInfo) -> bool:
2241
"""
2342
Check whether a choices type has any lazy strings for labels.
@@ -33,7 +52,8 @@ def _has_lazy_label(node: TypeInfo) -> bool:
3352
# If the empty label is lazy, then we don't need to check all the members.
3453
return True
3554

36-
for member_name in node.enum_members:
55+
# TODO: [mypy 1.14+] Use `node.enum_members` and remove `_get_enum_members()` backport.
56+
for member_name in _get_enum_members(node):
3757
if (sym := node.get(member_name)) is None:
3858
continue
3959

0 commit comments

Comments
 (0)