Skip to content
Closed
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
8 changes: 7 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3387,7 +3387,13 @@ def visit_block(self, b: Block) -> None:

self.accept(s)
# Clear expression caches after each statement to avoid unlimited growth.
self.expr_checker.reset()
# Don't do this for statements in a lambda body: a lambda is often checked
# several times as an argument of an enclosing generic call, and clearing the
# caches there makes checking nested calls with lambda arguments exponential.
# This is safe: the expression cache is never used inside lambdas, and the
# freshen cache is meant to be reused for the same call within a statement.
if not self.expr_checker.in_lambda_expr:
self.expr_checker.reset()

def should_report_unreachable_issues(self) -> bool:
return (
Expand Down
22 changes: 20 additions & 2 deletions test-data/unit/check-generics.test
Original file line number Diff line number Diff line change
Expand Up @@ -3104,8 +3104,8 @@ reveal_type(dec3(lambda x: x[0])) # N: Revealed type is "def [S] (S`6) -> S`6"
reveal_type(dec4(lambda x: [x])) # N: Revealed type is "def [S] (S`9) -> S`9"
reveal_type(dec1(lambda x: 1)) # N: Revealed type is "def (builtins.int) -> builtins.list[builtins.int]"
reveal_type(dec5(lambda x: x)) # N: Revealed type is "def (builtins.int) -> builtins.list[builtins.int]"
reveal_type(dec3(lambda x: x)) # N: Revealed type is "def [S] (S`17) -> builtins.list[S`17]"
reveal_type(dec4(lambda x: x)) # N: Revealed type is "def [T] (builtins.list[T`21]) -> T`21"
reveal_type(dec3(lambda x: x)) # N: Revealed type is "def [S] (S`15) -> builtins.list[S`15]"
reveal_type(dec4(lambda x: x)) # N: Revealed type is "def [T] (builtins.list[T`19]) -> T`19"
dec4_bound(lambda x: x) # E: Value of type variable "I" of "dec4_bound" cannot be "list[T]"
[builtins fixtures/list.pyi]

Expand Down Expand Up @@ -3789,3 +3789,21 @@ class C:
# The type variable id below must be small (say under a hundred).
reveal_type(C().meth) # N: Revealed type is "def [S] (x: S`91) -> S`91"
[builtins fixtures/tuple.pyi]

[case testExpressionCacheWorksInNestedCallWithLambdas]
from typing import Callable, Optional, TypeVar

T = TypeVar("T")
S = TypeVar("S")

def r(xs: list[T], fn: Callable[[T], S]) -> list[S]: ...

x: list[Optional[object]] = r(r(r(r(r(r(r(r([1], lambda a: a), lambda a: a), lambda a: a), lambda a: a), lambda a: a), lambda a: a), lambda a: a), lambda a: a)
reveal_type(r(r(r(r(r(r(r(r([1], lambda a: a), lambda a: a), lambda a: a), lambda a: a), lambda a: a), lambda a: a), lambda a: a), lambda a: a)) # N: Revealed type is "builtins.list[builtins.int]"

class C:
def meth(self, x: S) -> S: ...

# The type variable id below must be small (say under a hundred).
reveal_type(C().meth) # N: Revealed type is "def [S] (x: S`35) -> S`35"
[builtins fixtures/list.pyi]
Loading