Repository: python/mypy (Python)

Issue:
NotImplementedError: Cannot serialize ErasedType instance
<!--
  Use this form only if mypy reports an "INTERNAL ERROR" and/or gives a traceback.
  Please include the traceback and all other messages below (use `mypy --show-traceback`).
-->

**Crash Report**

I'm making a project that deals quite heavily with continuation-passing style, and I'm getting a crash.

**Traceback**

```
Traceback (most recent call last):
  File "~/.local/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "~/.local/lib/python3.8/site-packages/mypy/__main__.py", line 8, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "mypy/main.py", line 90, in main
  File "mypy/build.py", line 180, in build
  File "mypy/build.py", line 254, in _build
  File "mypy/build.py", line 2630, in dispatch
  File "mypy/build.py", line 2953, in process_graph
  File "mypy/build.py", line 3070, in process_stale_scc
  File "mypy/build.py", line 2232, in write_cache
  File "mypy/build.py", line 1445, in write_cache
  File "mypy/nodes.py", line 303, in serialize
  File "mypy/nodes.py", line 3098, in serialize
  File "mypy/nodes.py", line 3037, in serialize
  File "mypy/nodes.py", line 883, in serialize
  File "mypy/types.py", line 814, in serialize
  File "mypy/types.py", line 145, in serialize
    "must be a (non-strict) subclass "
NotImplementedError: Cannot serialize ErasedType instance
```

**To Reproduce**

```py
import typing as T

V = T.TypeVar("V")
V2 = T.TypeVar("V2")

def apply(val: V, func: T.Callable[[V], V2]) -> V2:
	return func(val)

xs = []
apply(0, lambda a: xs.append(a))
```

**Your Environment**

<!-- Include as many relevant details about the environment you experienced the bug in -->

- Mypy version used: `mypy 0.790`
- Mypy command-line flags: None (just `mypy error.py`)
- Mypy configuration options from `mypy.ini` (and other config files): None
- Python version used: `Python 3.8.6`
- Operating system and version: `Linux 5.1.5-arch1-2-ARCH`

<!--
You can freely edit this text, please remove all the lines
you believe are unnecessary.
-->



Hints:
Bisects to #8562
This still repros on master

Patch:
diff --git a/mypy/checker.py b/mypy/checker.py
--- a/mypy/checker.py
+++ b/mypy/checker.py
@@ -178,6 +178,7 @@
     AnyType,
     CallableType,
     DeletedType,
+    ErasedType,
     FunctionLike,
     Instance,
     LiteralType,
@@ -7031,11 +7032,15 @@ def is_valid_inferred_type(typ: Type, is_lvalue_final: bool = False) -> bool:
         return is_lvalue_final
     elif isinstance(proper_type, UninhabitedType):
         return False
-    return not typ.accept(NothingSeeker())
+    return not typ.accept(InvalidInferredTypes())
 
 
-class NothingSeeker(TypeQuery[bool]):
-    """Find any <nothing> types resulting from failed (ambiguous) type inference."""
+class InvalidInferredTypes(TypeQuery[bool]):
+    """Find type components that are not valid for an inferred type.
+
+    These include <Erased> type, and any <nothing> types resulting from failed
+    (ambiguous) type inference.
+    """
 
     def __init__(self) -> None:
         super().__init__(any)
@@ -7043,6 +7048,10 @@ def __init__(self) -> None:
     def visit_uninhabited_type(self, t: UninhabitedType) -> bool:
         return t.ambiguous
 
+    def visit_erased_type(self, t: ErasedType) -> bool:
+        # This can happen inside a lambda.
+        return True
+
 
 class SetNothingToAny(TypeTranslator):
     """Replace all ambiguous <nothing> types with Any (to avoid spurious extra errors)."""


Test patch:
diff --git a/test-data/unit/check-incremental.test b/test-data/unit/check-incremental.test
--- a/test-data/unit/check-incremental.test
+++ b/test-data/unit/check-incremental.test
@@ -6286,3 +6286,29 @@ class C: ...
 [out]
 [out2]
 [out3]
+
+[case testNoCrashOnPartialLambdaInference]
+import m
+[file m.py]
+from typing import TypeVar, Callable
+
+V = TypeVar("V")
+def apply(val: V, func: Callable[[V], None]) -> None:
+    return func(val)
+
+xs = []
+apply(0, lambda a: xs.append(a))
+[file m.py.2]
+from typing import TypeVar, Callable
+
+V = TypeVar("V")
+def apply(val: V, func: Callable[[V], None]) -> None:
+    return func(val)
+
+xs = []
+apply(0, lambda a: xs.append(a))
+reveal_type(xs)
+[builtins fixtures/list.pyi]
+[out]
+[out2]
+tmp/m.py:9: note: Revealed type is "builtins.list[builtins.int]"