Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash on partial type inference within a lambda #14087

Merged
merged 3 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
AnyType,
CallableType,
DeletedType,
ErasedType,
FunctionLike,
Instance,
LiteralType,
Expand Down Expand Up @@ -7031,18 +7032,26 @@ 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)

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)."""
Expand Down
26 changes: 26 additions & 0 deletions test-data/unit/check-incremental.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]"