Repository navigation
[mypyc] Fix crash on nested function in a method of a non-native class - #22237
Merged
Merged
Conversation
Fixes mypyc/mypyc#1225. Fixes mypyc/mypyc#1205. Compiling a method of a non-native class that contains a nested function or a lambda crashed with an internal error: ```python from mypy_extensions import mypyc_attr @mypyc_attr(native_class=False) class C: def f(self) -> Callable[[], C]: return lambda: self # AssertionError ``` Methods of non-native classes are compiled to callable classes, like nested functions. If a method contains a nested function or a lambda, the method creates an environment object, which holds the variables captured by the nested function (`self` above) and the nested function objects. When adding these to the environment, the IR builder treated the method like a nested function and looked for the environment via the callable class, but only nested functions set it up there. Now the method's own environment is used, as in other functions that aren't nested.
p-sawicki
approved these changes
Oct 20, 2026
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 mypyc/mypyc#1225. Fixes mypyc/mypyc#1205.
Compiling a method of a non-native class that contains a nested function or a lambda crashed with an internal error:
Methods of non-native classes are compiled to callable classes, like nested functions. If a method contains a nested function or a lambda, the method creates an environment object, which holds the variables captured by the nested function (
selfabove) and the nested function objects. When adding these to the environment, the IR builder treated the method like a nested function and looked for the environment via the callable class, but only nested functions set it up there. Now the method's own environment is used, as in other functions that aren't nested.