Skip to content

perf: try to cache inner contexts of overloads #19408

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

Closed
Prev Previous commit
Next Next commit
Fix selfcheck
  • Loading branch information
sterliakov committed Jul 8, 2025
commit 03af5b21d2c1c93b25fdd381687cc722bcf1c062
24 changes: 17 additions & 7 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,9 @@ def __init__(
self._arg_infer_context_cache = None

self.overload_stack_depth = 0
self._args_cache = {}
self.ops_stack_depth = 0
self._args_cache: dict[tuple[int, ...], list[Type]] = {}
self._ops_cache: dict[int, Type] = {}

def reset(self) -> None:
self.resolved_type = {}
Expand Down Expand Up @@ -1616,7 +1618,7 @@ def check_call(
object_type,
)
elif isinstance(callee, Overloaded):
with self.overload_context(callee.name()):
with self.overload_context():
return self.check_overload_call(
callee, args, arg_kinds, arg_names, callable_name, object_type, context
)
Expand Down Expand Up @@ -1679,13 +1681,21 @@ def check_call(
return self.msg.not_callable(callee, context), AnyType(TypeOfAny.from_error)

@contextmanager
def overload_context(self, fn):
def overload_context(self) -> Iterator[None]:
self.overload_stack_depth += 1
yield
self.overload_stack_depth -= 1
if self.overload_stack_depth == 0:
self._args_cache.clear()

@contextmanager
def ops_context(self) -> Iterator[None]:
self.ops_stack_depth += 1
yield
self.ops_stack_depth -= 1
if self.ops_stack_depth == 0:
self._ops_cache.clear()

def check_callable_call(
self,
callee: CallableType,
Expand Down Expand Up @@ -3493,8 +3503,8 @@ def visit_op_expr(self, e: OpExpr) -> Type:
return self.strfrm_checker.check_str_interpolation(e.left, e.right)

key = id(e)
if key in self._args_cache:
return self._args_cache[key]
if key in self._ops_cache:
return self._ops_cache[key]
left_type = self.accept(e.left)

proper_left_type = get_proper_type(left_type)
Expand Down Expand Up @@ -3564,7 +3574,7 @@ def visit_op_expr(self, e: OpExpr) -> Type:
)

if e.op in operators.op_methods:
with self.overload_context(e.op):
with self.ops_context():
method = operators.op_methods[e.op]
if use_reverse is UseReverse.DEFAULT or use_reverse is UseReverse.NEVER:
result, method_type = self.check_op(
Expand All @@ -3586,7 +3596,7 @@ def visit_op_expr(self, e: OpExpr) -> Type:
else:
assert_never(use_reverse)
e.method_type = method_type
self._args_cache[key] = result
self._ops_cache[key] = result
return result
else:
raise RuntimeError(f"Unknown operator {e.op}")
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy