Skip to content

Commit 479550b

Browse files
committed
Poison cache when we encounter any lambda
1 parent 0f0e1c7 commit 479550b

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

mypy/checkexpr.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@
233233
"builtins.memoryview",
234234
}
235235

236+
POISON_KEY: Final = (-1,)
237+
236238

237239
class TooManyUnions(Exception):
238240
"""Indicates that we need to stop splitting unions in an attempt
@@ -1954,8 +1956,10 @@ def infer_arg_types_in_empty_context(self, args: list[Expression]) -> list[Type]
19541956
# We can only use this hack locally while checking a single nested overloaded
19551957
# call. This saves a lot of rechecking, but is not generally safe. Cache is
19561958
# pruned upon leaving the outermost overload.
1957-
can_cache = self.overload_stack_depth > 0 and not any(
1958-
isinstance(t, TempNode) for t in args
1959+
can_cache = (
1960+
self.overload_stack_depth > 0
1961+
and POISON_KEY not in self._args_cache
1962+
and not any(isinstance(t, TempNode) for t in args)
19591963
)
19601964
key = tuple(map(id, args))
19611965
if can_cache and key in self._args_cache:
@@ -5426,6 +5430,9 @@ def find_typeddict_context(
54265430

54275431
def visit_lambda_expr(self, e: LambdaExpr) -> Type:
54285432
"""Type check lambda expression."""
5433+
if self.overload_stack_depth > 0:
5434+
# Poison cache when we encounter lambdas - it isn't safe to cache their types.
5435+
self._args_cache[POISON_KEY] = []
54295436
self.chk.check_default_args(e, body_is_trivial=False)
54305437
inferred_type, type_override = self.infer_lambda_type_using_context(e)
54315438
if not inferred_type:

0 commit comments

Comments
 (0)