Repository navigation
Fix crash on __call__ attribute whose type refers back to its class - #22362
Open
yvaishalirao wants to merge 1 commit into
Open
yvaishalirao wants to merge 1 commit into
yvaishalirao wants to merge 1 commit into
Conversation
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.
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #22361.
Calling an instance whose class declares
__call__as an attribute of its own type crashed mypy:In
check_call(), anInstancecallee is handled by looking up__call__and callingcheck_call()on the result. When that result leads back to the same class, this recurses forever. This also happens withC | 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 callablewhen 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.