From 31239071540d480fbab8ddf533a3ba4c6b156609 Mon Sep 17 00:00:00 2001 From: yvaishalirao Date: Wed, 4 Nov 2026 00:08:33 +0530 Subject: [PATCH] Fix crash on __call__ attribute whose type refers back to its class Calling an instance whose class declares `__call__` as an attribute of its own type (e.g. `__call__: C`, `__call__: C | None` or `__call__: Self | None`) made check_call() recurse forever, resulting in a RecursionError or a segfault in compiled mypy. Track the classes whose `__call__` is being resolved and report "not callable" when a class is reached again. Fixes #22361. --- mypy/checkexpr.py | 39 ++++++++++++++++++++++--------- test-data/unit/check-classes.test | 39 +++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 11 deletions(-) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 2ed757eeeaa83..0d450a9d8413d 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -342,6 +342,9 @@ def __init__( ] = {} self.freshen_cache: dict[tuple[Context, CallableType], CallableType] = {} self.in_lambda_expr = False + # Classes whose "__call__" is currently being resolved in check_call(). used to + # detect a "__call__" attribute whose type leads back to the same class + self.resolving_call_types: set[TypeInfo] = set() self._literal_true: Instance | None = None self._literal_false: Instance | None = None @@ -507,7 +510,13 @@ def visit_call_expr(self, e: CallExpr, allow_none_return: bool = False) -> Type: self.visit_call_expr_inner(e) # It's really a special form that only looks like a call. return self.accept(e.analyzed, self.type_context[-1]) - return self.visit_call_expr_inner(e, allow_none_return=allow_none_return) + # Nested call expressions (e.g. in arguments) resolve "__call__" independently + outer_resolving_call_types = self.resolving_call_types + self.resolving_call_types = set() + try: + return self.visit_call_expr_inner(e, allow_none_return=allow_none_return) + finally: + self.resolving_call_types = outer_resolving_call_types def refers_to_typeddict(self, base: Expression) -> bool: if not isinstance(base, RefExpr): @@ -1624,6 +1633,10 @@ def check_call( elif isinstance(callee, UnionType): return self.check_union_call(callee, args, arg_kinds, arg_names, context) elif isinstance(callee, Instance): + if callee.type in self.resolving_call_types: + # "__call__" is (transitivly) declared with the type of its own class + # e.g. "__call__: C" in class C. Following it would recurse forever + return self.msg.not_callable(callee, context), AnyType(TypeOfAny.from_error) call_function = analyze_member_access( "__call__", callee, @@ -1640,16 +1653,20 @@ def check_call( call_function = self.transform_callee_type( callable_name, call_function, args, arg_kinds, context, arg_names, callee ) - result = self.check_call( - call_function, - args, - arg_kinds, - context, - arg_names, - callable_node, - callable_name, - callee, - ) + self.resolving_call_types.add(callee.type) + try: + result = self.check_call( + call_function, + args, + arg_kinds, + context, + arg_names, + callable_node, + callable_name, + callee, + ) + finally: + self.resolving_call_types.discard(callee.type) if callable_node: # check_call() stored "call_function" as the type, which is incorrect. # Override the type. diff --git a/test-data/unit/check-classes.test b/test-data/unit/check-classes.test index a89a5f3690281..d1f0d45897149 100644 --- a/test-data/unit/check-classes.test +++ b/test-data/unit/check-classes.test @@ -3409,6 +3409,45 @@ if int(): if int(): b = a(a) # E: Incompatible types in assignment (expression has type "A", variable has type "B") +[case testCallAttributeOfOwnTypeNoCrash] +from typing import Generic, Optional, TypeVar +from typing_extensions import Self + +T = TypeVar("T") + +class A: + __call__: A +class B: + __call__: Optional[B] +class S: + __call__: Optional[Self] +class G(Generic[T]): + __call__: G[list[T]] +class X: + __call__: Y +class Y: + __call__: X + +def f(a: A, b: B, s: S, g: G[int], x: X) -> None: + a() # E: "A" not callable + b() # E: "B" not callable \ + # E: "None" not callable + s() # E: "S" not callable \ + # E: "None" not callable + g() # E: "G[list[int]]" not callable + x() # E: "X" not callable +[builtins fixtures/list.pyi] + +[case testCallAttributeOfCallableTypeNestedCall] +class D: + def __call__(self, a: int) -> int: return a +class E: + __call__: D + +def f(d: D, e: E) -> None: + reveal_type(d(d(1))) # N: Revealed type is "builtins.int" + reveal_type(e(e(1))) # N: Revealed type is "builtins.int" + -- __new__ -- --------