Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

catch now raises typeerror on async handler #69

Merged
merged 5 commits into from
Jul 12, 2023
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
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ Version history

This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.

**UNRELEASED**
- `catch()` now raises a `TypeError` if passed an async exception handler instead of
just giving a `RuntimeWarning` about the coroutine never being awaited. (#66, PR by
John Litborn)

**1.1.2**

- Changed handling of exceptions in exception group handler callbacks to not wrap a
Expand Down
9 changes: 8 additions & 1 deletion src/exceptiongroup/_catch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import inspect
import sys
from collections.abc import Callable, Iterable, Mapping
from contextlib import AbstractContextManager
Expand Down Expand Up @@ -49,9 +50,15 @@ def handle_exception(self, exc: BaseException) -> BaseException | None:
matched, excgroup = excgroup.split(exc_types)
if matched:
try:
handler(matched)
result = handler(matched)
except BaseException as new_exc:
new_exceptions.append(new_exc)
else:
if inspect.iscoroutine(result):
raise TypeError(
f"Error trying to handle {matched!r} with {handler!r}. "
"Exception handler must be a sync function."
) from exc

if not excgroup:
break
Expand Down
14 changes: 14 additions & 0 deletions tests/test_catch.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,17 @@ def test_catch_subclass():
assert isinstance(lookup_errors[0], ExceptionGroup)
exceptions = lookup_errors[0].exceptions
assert isinstance(exceptions[0], KeyError)


def test_async_handler(request):
async def handler(eg):
pass

def delegate(eg):
coro = handler(eg)
request.addfinalizer(coro.close)
return coro

with pytest.raises(TypeError, match="Exception handler must be a sync function."):
with catch({TypeError: delegate}):
raise ExceptionGroup("message", TypeError("uh-oh"))