Repository: typeddjango/django-stubs

Issue:
Crash when writing cache due to unresolved placeholder
# Bug report

## What's wrong

On the latest master mypy will crash when writing the cache when checking certain files. This does not happen when checking the full project, so I assume it's a missing `.defer()` call somewhere. The file being serialized is not the one that's being checked.

```
LOG:  Writing <snip>.models <snip>/models.py <snip>/models.meta.json <snip>/models.data.json
LOG:  Build finished in 10.155 seconds with 1844 modules, and 0 errors
Traceback (most recent call last):
  File "<snip>/.venv/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "<snip>/.venv/lib/python3.10/site-packages/mypy/__main__.py", line 12, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "mypy/main.py", line 96, in main
  File "mypy/main.py", line 173, in run_build
  File "mypy/build.py", line 154, in build
  File "mypy/build.py", line 230, in _build
  File "mypy/build.py", line 2729, in dispatch
  File "mypy/build.py", line 3087, in process_graph
  File "mypy/build.py", line 3205, in process_stale_scc
  File "mypy/build.py", line 2313, in write_cache
  File "mypy/build.py", line 1470, in write_cache
  File "mypy/nodes.py", line 352, in serialize
  File "mypy/nodes.py", line 3435, in serialize
  File "mypy/nodes.py", line 3372, in serialize
  File "mypy/nodes.py", line 2893, in serialize
  File "mypy/nodes.py", line 1039, in serialize
  File "mypy/types.py", line 536, in serialize
  File "mypy/types.py", line 2530, in serialize
AssertionError: Internal error: unresolved placeholder type <snip>.managers.FlagDeleteOwnedModel
```

## How is that should be

Mypy shouldn't crash 🙃 

## System information

- OS:
- `python` version: 3.10.4
- `django` version: 3.2.14
- `mypy` version: 0.961
- `django-stubs` version: latest `master`
- `django-stubs-ext` version: not used



Discussion:

Resolving patch:
diff --git a/mypy_django_plugin/transformers/managers.py b/mypy_django_plugin/transformers/managers.py
--- a/mypy_django_plugin/transformers/managers.py
+++ b/mypy_django_plugin/transformers/managers.py
@@ -16,6 +16,7 @@
     Var,
 )
 from mypy.plugin import AttributeContext, DynamicClassDefContext, SemanticAnalyzerPluginInterface
+from mypy.semanal import has_placeholder
 from mypy.types import AnyType, CallableType, Instance, ProperType
 from mypy.types import Type as MypyType
 from mypy.types import TypeOfAny
@@ -246,7 +247,10 @@ def create_manager_info_from_from_queryset_call(
     else:
         manager_name = f"{base_manager_info.name}From{queryset_info.name}"
 
-    new_manager_info = create_manager_class(api, base_manager_info, name or manager_name, call_expr.line)
+    try:
+        new_manager_info = create_manager_class(api, base_manager_info, name or manager_name, call_expr.line)
+    except helpers.IncompleteDefnException:
+        return None
 
     popuplate_manager_from_queryset(new_manager_info, queryset_info)
 
@@ -272,6 +276,10 @@ def create_manager_class(
     base_manager_instance = fill_typevars(base_manager_info)
     assert isinstance(base_manager_instance, Instance)
 
+    # If any of the type vars are undefined we need to defer. This is handled by the caller
+    if any(has_placeholder(type_var) for type_var in base_manager_info.defn.type_vars):
+        raise helpers.IncompleteDefnException
+
     manager_info = helpers.create_type_info(name, api.cur_mod_id, bases=[base_manager_instance])
     manager_info.line = line
     manager_info.type_vars = base_manager_info.type_vars


Test patch: