Skip to content

[mypyc] Fix crash when '__enter__'/'__exit__' inherited from Python class - #22227

Merged
JukkaL merged 1 commit into
masterfrom
mypyc-bulkfix-1193-inherited-enter
Oct 19, 2026
Merged

JukkaL merged 1 commit into
masterfrom
mypyc-bulkfix-1193-inherited-enter

Conversation

@JukkaL

@JukkaL JukkaL commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator

Fixes mypyc/mypyc#1193.

Compiling a with statement crashed if the context manager was a native class that inherited __enter__ or __exit__ from a non-native class:

from contextlib import AbstractContextManager

class C(AbstractContextManager):
    def __exit__(self, exc_type, exc_value, traceback) -> None:
        pass

with C() as c:  # KeyError: "'C' has no attribute '__enter__'"
    pass

Now the methods are only called natively if both are defined in a native class.

…lass

Fixes mypyc/mypyc#1193.

Compiling a `with` statement crashed if the context manager was a native
class that inherited `__enter__` or `__exit__` from a non-native class:

```python
from contextlib import AbstractContextManager

class C(AbstractContextManager):
    def __exit__(self, exc_type, exc_value, traceback) -> None:
        pass

with C() as c:  # KeyError: "'C' has no attribute '__enter__'"
    pass
```

Now the methods are only called natively if both are defined in a
native class, and otherwise the generic code path is used.
@JukkaL
JukkaL merged commit 9f56386 into master Oct 19, 2026
18 checks passed
@JukkaL
JukkaL deleted the mypyc-bulkfix-1193-inherited-enter branch October 19, 2026 12:09
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.

Subclass of AbstractContextManager without __enter__ override fails to transpile

2 participants