Skip to content

Commit cf59b82

Browse files
authored
Fix crash on partial type inference within a lambda (#14087)
Fixes #9654 Seems to be quite straightforward. Erased types should never be stored on variables, it is just a temporary thing for nested generic calls.
1 parent 57ce73d commit cf59b82

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

mypy/checker.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@
178178
AnyType,
179179
CallableType,
180180
DeletedType,
181+
ErasedType,
181182
FunctionLike,
182183
Instance,
183184
LiteralType,
@@ -7040,18 +7041,26 @@ def is_valid_inferred_type(typ: Type, is_lvalue_final: bool = False) -> bool:
70407041
return is_lvalue_final
70417042
elif isinstance(proper_type, UninhabitedType):
70427043
return False
7043-
return not typ.accept(NothingSeeker())
7044+
return not typ.accept(InvalidInferredTypes())
70447045

70457046

7046-
class NothingSeeker(TypeQuery[bool]):
7047-
"""Find any <nothing> types resulting from failed (ambiguous) type inference."""
7047+
class InvalidInferredTypes(TypeQuery[bool]):
7048+
"""Find type components that are not valid for an inferred type.
7049+
7050+
These include <Erased> type, and any <nothing> types resulting from failed
7051+
(ambiguous) type inference.
7052+
"""
70487053

70497054
def __init__(self) -> None:
70507055
super().__init__(any)
70517056

70527057
def visit_uninhabited_type(self, t: UninhabitedType) -> bool:
70537058
return t.ambiguous
70547059

7060+
def visit_erased_type(self, t: ErasedType) -> bool:
7061+
# This can happen inside a lambda.
7062+
return True
7063+
70557064

70567065
class SetNothingToAny(TypeTranslator):
70577066
"""Replace all ambiguous <nothing> types with Any (to avoid spurious extra errors)."""

test-data/unit/check-incremental.test

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6286,3 +6286,29 @@ class C: ...
62866286
[out]
62876287
[out2]
62886288
[out3]
6289+
6290+
[case testNoCrashOnPartialLambdaInference]
6291+
import m
6292+
[file m.py]
6293+
from typing import TypeVar, Callable
6294+
6295+
V = TypeVar("V")
6296+
def apply(val: V, func: Callable[[V], None]) -> None:
6297+
return func(val)
6298+
6299+
xs = []
6300+
apply(0, lambda a: xs.append(a))
6301+
[file m.py.2]
6302+
from typing import TypeVar, Callable
6303+
6304+
V = TypeVar("V")
6305+
def apply(val: V, func: Callable[[V], None]) -> None:
6306+
return func(val)
6307+
6308+
xs = []
6309+
apply(0, lambda a: xs.append(a))
6310+
reveal_type(xs)
6311+
[builtins fixtures/list.pyi]
6312+
[out]
6313+
[out2]
6314+
tmp/m.py:9: note: Revealed type is "builtins.list[builtins.int]"

0 commit comments

Comments
 (0)