Skip to content

[mypyc] Fix crash on nested function in a method of a non-native class - #22237

Merged
JukkaL merged 1 commit into
masterfrom
mypyc-bulkfix-1225-non-ext-nested-func
Oct 20, 2026
Merged

JukkaL merged 1 commit into
masterfrom
mypyc-bulkfix-1225-non-ext-nested-func

Conversation

@JukkaL

@JukkaL JukkaL commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

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:

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.

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.
@JukkaL
JukkaL merged commit eaeb098 into master Oct 20, 2026
18 checks passed
@JukkaL
JukkaL deleted the mypyc-bulkfix-1225-non-ext-nested-func branch October 20, 2026 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants