Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mypyc/irbuild/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from mypy.types import (
AnyType,
CallableType,
DeletedType,
Instance,
LiteralType,
NoneTyp,
Expand Down Expand Up @@ -166,6 +167,9 @@ def type_to_rtype(self, typ: Type | None) -> RType:
elif isinstance(typ, (UninhabitedType, UnboundType)):
# Sure, whatever!
return object_rprimitive
elif isinstance(typ, DeletedType):
# The type of an exception variable after the except block, for example
return object_rprimitive

# I think we've covered everything that is supposed to
# actually show up, so anything else is a bug somewhere.
Expand Down
29 changes: 29 additions & 0 deletions mypyc/test-data/run-exceptions.test
Original file line number Diff line number Diff line change
Expand Up @@ -665,3 +665,32 @@ from typing import Any
class OtherException(Exception):
def __new__(cls) -> Any:
return ValueError("other")

[case testExceptionVariableCapturedByNestedFunction]
from typing import Callable, Iterator

def call(f: Callable[[], str]) -> str:
return f()

def test_lambda() -> None:
try:
raise RuntimeError("x")
except RuntimeError as exc:
assert call(lambda: str(exc)) == "x"

def test_nested_function() -> None:
try:
raise ValueError("y")
except Exception as exc:
def f() -> str:
return type(exc).__name__ + str(exc)
assert call(f) == "ValueErrory"

def gen() -> Iterator[str]:
try:
raise KeyError("z")
except KeyError as exc:
yield call(lambda: repr(exc))

def test_generator() -> None:
assert list(gen()) == ["KeyError('z')"]
Loading