Skip to content

[mypyc] Fix crash when exception variable is captured by nested function - #22249

Merged
JukkaL merged 1 commit into
masterfrom
mypyc-bulkfix-1184-captured-exc-var
Oct 21, 2026
Merged

JukkaL merged 1 commit into
masterfrom
mypyc-bulkfix-1184-captured-exc-var

Conversation

@JukkaL

@JukkaL JukkaL commented Oct 21, 2026

Copy link
Copy Markdown
Collaborator

Fixes mypyc/mypyc#1184.

This crashed with "unexpected type DeletedType":

def f() -> None:
    try:
        raise RuntimeError()
    except RuntimeError as exc:
        fn = lambda: print(exc)

Mypy gives the variable of an except clause the type DeletedType after the except block, and this type is used when adding a captured variable to the environment class of the function. Now we map DeletedType to object.

Fixes mypyc/mypyc#1184.

This crashed with "unexpected type DeletedType":

```python
def f() -> None:
    try:
        raise RuntimeError()
    except RuntimeError as exc:
        fn = lambda: print(exc)
```

Mypy gives the variable of an except clause the type `DeletedType`
after the except block, and this type is used when adding a captured
variable to the environment class of the function. Now we map
`DeletedType` to `object`.
@JukkaL
JukkaL merged commit 186e1cd into master Oct 21, 2026
18 checks passed
@JukkaL
JukkaL deleted the mypyc-bulkfix-1184-captured-exc-var branch October 21, 2026 13:26
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.

Exception variable used in nested function crashes mypyc

2 participants