⚡ Sangsakala Proxy 🏠 Home
Skip to content

Fix crash on __call__ attribute whose type refers back to its class - #22362

Open
yvaishalirao wants to merge 1 commit into
python:masterfrom
yvaishalirao:fix-call-attr-self-recursion
Open

yvaishalirao wants to merge 1 commit into
python:masterfrom
yvaishalirao:fix-call-attr-self-recursion

Conversation

@yvaishalirao

Copy link
Copy Markdown

Fixes #22361.

Calling an instance whose class declares __call__ as an attribute of its own type crashed mypy:

class C:
    __call__: "C"

def f(x: C) -> None:
    x()  # RecursionError / segfault

In check_call(), an Instance callee is handled by looking up __call__ and calling check_call() on the result. When that result leads back to the same class, this recurses forever. This also happens with C | None, Self | None, generic classes such as __call__: G[list[T]], and mutually referencing classes.

The fix tracks the classes (TypeInfos) whose __call__ is currently being resolved and reports "C" not callable when one is reached again. Each call expression starts with a fresh set, so nested calls in arguments (e.g. d(d(1))) are unaffected.

Tests cover all the variants above, plus a regression test that normal and nested callable objects still work.

I used an LLM (Claude) to help investigate and draft this change; I have reviewed and tested it.

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 python#22361.
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Segfault / RecursionError when calling an instance whose class declares __call__ as an attribute of its own type

1 participant