Repository: typeddjango/django-stubs

Issue:
"cannot import name 'parse_bool'" after updating somes packages
# Bug report

I use this version of mypy

  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: v1.6.1

## What's wrong

mypy.....................................................................Failed
- hook id: mypy
- exit code: 2

setup.cfg:34: error: Error importing plugin "mypy_django_plugin.main": cannot import name 'parse_bool' from 'mypy.semanal_shared' (/home/runner/.cache/pre-commit/repo712jqpu7/py_env-python3/lib/python3.11/site-packages/mypy/semanal_shared.cpython-311-x86_64-linux-gnu.so)  [misc]
Found 1 error in 1 file (errors prevented further checking)

## System information

- OS:
- `python` version: 3.11
- `django` version: 4.2.5
- `mypy` version: 1.6.1
- `django-stubs` version: 4.2.4
- `django-stubs-ext` version: 3.2.3



Discussion:
Probably related to https://github.com/typeddjango/django-stubs/pull/1703. @flaeppe any ideas?
Not having any ideas really, I'm still seeing a `mypy.semanal_shared.parse_bool` for `v1.6.1`.

Ref: https://github.com/python/mypy/blob/v1.6.1/mypy/semanal_shared.py#L469-L475
Might be some quirk related to the way mypy is compiled with Mypyc

> name 'parse_bool' from 'mypy.semanal_shared' (/home/runner/.cache/pre-commit/repo712jqpu7/py_env-python3/lib/python3.11/site-packages/mypy/**semanal_shared.cpython-311-x86_64-linux-gnu.so**)

If we don't understand this, may make sense to report a mypyc issue: https://github.com/mypyc/mypyc/issues
Yeah, that could probably be a reason. Would probably be worth to take it for a spin upstream, see what they say.
Created upstream issue:

* https://github.com/mypyc/mypyc/issues/1026
I am trying to resolve it on my side playing with versions.

But thanks for the quick reaction!
If there's no resolution in a few days, I'd propose reverting #1703 for the upcoming 4.2.6 release #1780

Resolving patch:
diff --git a/mypy_django_plugin/lib/helpers.py b/mypy_django_plugin/lib/helpers.py
--- 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
--- 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
--- 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
 
 


Test patch: