From 6b42fd9edd1a74bd5b4f35793e0d56f64922b529 Mon Sep 17 00:00:00 2001 From: Jahongir Qurbonov Date: Sat, 14 Nov 2026 16:10:47 +0500 Subject: [PATCH] Don't clear expression caches inside lambda bodies Checking chained generic calls with lambda arguments, like r(r(r([1], lambda a: a), lambda a: a), lambda a: a), is exponential in the nesting depth. The two-sided inference from #22394 re-infers all arguments once more when there is a lambda argument, which raised the number of lambda visits from about 2.4^n to 3^n (29523 visits and ~12s for n=9). Repeated checks of the same inner call in the same type context should be served by the expression cache, but it was never hit here: the statement loop in visit_block() resets the expression checker caches after each statement, and a lambda body is checked as a block with a return statement. So checking any lambda argument wiped both the expression cache and the freshen cache, and every re-check of an inner call started from scratch (with freshly renamed type variables, so even its type contexts did not match old cache keys). Skip the reset for statements in a lambda body. The expression cache is never read or written while in_lambda_expr is set, so its entries cannot become stale there; the caches are still cleared after the enclosing statement. The example above now visits lambdas 6n-3 times (n=9: 29523 -> 51). The ids of type variables in testInferenceAgainstGenericLambdas are now slightly smaller, since fewer redundant freshenings happen; the inferred types are unchanged. --- mypy/checker.py | 8 +++++++- test-data/unit/check-generics.test | 22 ++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/mypy/checker.py b/mypy/checker.py index ea7adc757d1d3..9f6442eaf937e 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -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 ( diff --git a/test-data/unit/check-generics.test b/test-data/unit/check-generics.test index 63282a979462e..608b5fe526dab 100644 --- a/test-data/unit/check-generics.test +++ b/test-data/unit/check-generics.test @@ -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] @@ -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]