diff --git a/mypy_django_plugin/lib/helpers.py b/mypy_django_plugin/lib/helpers.py index e90498e9b..b262ea585 100644 --- a/mypy_django_plugin/lib/helpers.py +++ b/mypy_django_plugin/lib/helpers.py @@ -35,7 +35,6 @@ SemanticAnalyzerPluginInterface, ) from mypy.semanal import SemanticAnalyzer -from mypy.semanal_shared import parse_bool from mypy.types import AnyType, Instance, LiteralType, NoneTyp, TupleType, TypedDictType, TypeOfAny, UnionType from mypy.types import Type as MypyType from typing_extensions import TypedDict @@ -176,6 +175,16 @@ def make_optional(typ: MypyType) -> MypyType: return UnionType.make_union([typ, NoneTyp()]) +# Duplicating mypy.semanal_shared.parse_bool because importing it directly caused ImportError (#1784) +def parse_bool(expr: Expression) -> Optional[bool]: + if isinstance(expr, NameExpr): + if expr.fullname == "builtins.True": + return True + if expr.fullname == "builtins.False": + return False + return None + + def has_any_of_bases(info: TypeInfo, bases: Iterable[str]) -> bool: for base_fullname in bases: if info.has_base(base_fullname): diff --git a/mypy_django_plugin/transformers/fields.py b/mypy_django_plugin/transformers/fields.py index 15a8dc43a..98654e749 100644 --- a/mypy_django_plugin/transformers/fields.py +++ b/mypy_django_plugin/transformers/fields.py @@ -6,13 +6,13 @@ from django.db.models.fields.reverse_related import ForeignObjectRel from mypy.nodes import AssignmentStmt, NameExpr, TypeInfo from mypy.plugin import FunctionContext -from mypy.semanal_shared import parse_bool from mypy.types import AnyType, Instance, ProperType, TypeOfAny, UnionType from mypy.types import Type as MypyType from mypy_django_plugin.django.context import DjangoContext from mypy_django_plugin.exceptions import UnregisteredModelError from mypy_django_plugin.lib import fullnames, helpers +from mypy_django_plugin.lib.helpers import parse_bool from mypy_django_plugin.transformers import manytomany if TYPE_CHECKING: diff --git a/mypy_django_plugin/transformers/querysets.py b/mypy_django_plugin/transformers/querysets.py index 50f3f81b8..f7e7933d4 100644 --- a/mypy_django_plugin/transformers/querysets.py +++ b/mypy_django_plugin/transformers/querysets.py @@ -7,14 +7,13 @@ from django.db.models.fields.reverse_related import ForeignObjectRel from mypy.nodes import ARG_NAMED, ARG_NAMED_OPT, Expression from mypy.plugin import FunctionContext, MethodContext -from mypy.semanal_shared import parse_bool from mypy.types import AnyType, Instance, TupleType, TypedDictType, TypeOfAny, get_proper_type from mypy.types import Type as MypyType from mypy_django_plugin.django.context import DjangoContext, LookupsAreUnsupported from mypy_django_plugin.lib import fullnames, helpers from mypy_django_plugin.lib.fullnames import ANY_ATTR_ALLOWED_CLASS_FULLNAME -from mypy_django_plugin.lib.helpers import is_annotated_model_fullname +from mypy_django_plugin.lib.helpers import is_annotated_model_fullname, parse_bool from mypy_django_plugin.transformers.models import get_or_create_annotated_type