Skip to content

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

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

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. ✅

@chrikrah chrikrah left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yvaishalirao #22361 is fixed here, mutual recursion included. From my perspective this can be merged.

# git archive 3123907 (parent 623bef9), Python 3.12, mypy.__file__ from the export
$ python -m pytest -q -n 0 mypy/test/testcheck.py -k check-classes.test
620 passed, 7719 deselected, 1 xfailed
# mypy/checkexpr.py from 623bef9, test data kept
FAILED check-classes.test::testCallAttributeOfOwnTypeNoCrash   # RecursionError

$ mypy --no-incremental probe.py   # class A: __call__: "B" / class B: __call__: "A"; a()
probe.py:11: error: "A" not callable  [misc]

$ mypy --no-incremental probe2.py   # class C: __call__: "C"; def h(cb: Callable[[], int]); h(C())
probe2.py:5: error: INTERNAL ERROR   # same on 623bef9, RecursionError via subtypes.py:383
# not run: the full test suite, mypyc

non-blocking: h(C()) still crashes in subtypes.py, yet Fixes #22361 closes the issue. Track that separately?

@yvaishalirao

Copy link
Copy Markdown
Author

@chrikrah thanks for the review! Agreed, the h(C()) crash goes through subtypes.py rather than check_call, so it makes sense to handle it separately. I see youve opened #22398 as well. Thanks for tracking that!

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

3 participants