-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feat][Core] Support ExceptionGroup (#47887)
See the description in the corresponding issue for details. The issue states that we should support `BaseExceptionGroup`, but I found that we don't support `BaseException` either, so it doesn't make sense to support `BaseExceptionGroup`. For `ExceptionGroup`, we need to override its `__new__` method as well. See https://docs.python.org/3/library/exceptions.html#ExceptionGroup for details. Signed-off-by: Chi-Sheng Liu <chishengliu@chishengliu.com> Co-authored-by: Ruiyang Wang <56065503+rynewang@users.noreply.github.com>
- Loading branch information
1 parent
4dfa033
commit 0056097
Showing
4 changed files
with
263 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
import os | ||
import sys | ||
from textwrap import dedent | ||
|
||
import pytest | ||
|
||
import ray | ||
from ray.exceptions import RayTaskError | ||
|
||
pytestmark = pytest.mark.skipif( | ||
sys.version_info < (3, 11), | ||
reason="ExceptionGroup is only available in Python 3.11+", | ||
) | ||
|
||
|
||
def test_baseexceptiongroup_task(ray_start_regular): | ||
baseexceptiongroup = BaseExceptionGroup( # noqa: F821 | ||
"test baseexceptiongroup", [BaseException("abc")] | ||
) | ||
|
||
@ray.remote | ||
def task(): | ||
raise baseexceptiongroup | ||
|
||
with pytest.raises(ray.exceptions.WorkerCrashedError): | ||
ray.get(task.remote()) | ||
|
||
|
||
def test_baseexceptiongroup_actor(ray_start_regular): | ||
baseexceptiongroup = BaseExceptionGroup( # noqa: F821 | ||
"test baseexceptiongroup", [BaseException("abc")] | ||
) | ||
|
||
@ray.remote | ||
class Actor: | ||
def f(self): | ||
raise baseexceptiongroup | ||
|
||
with pytest.raises(ray.exceptions.ActorDiedError): | ||
a = Actor.remote() | ||
ray.get(a.f.remote()) | ||
|
||
|
||
def test_except_exceptiongroup(ray_start_regular): | ||
exceptiongroup = ExceptionGroup( # noqa: F821 | ||
"test exceptiongroup", [ValueError(), TypeError()] | ||
) | ||
|
||
@ray.remote | ||
def task(): | ||
raise exceptiongroup | ||
|
||
@ray.remote | ||
class Actor: | ||
def f(self): | ||
raise exceptiongroup | ||
|
||
try: | ||
ray.get(task.remote()) | ||
except Exception as ex: | ||
assert isinstance(ex, RayTaskError) | ||
assert isinstance(ex, ExceptionGroup) # noqa: F821 | ||
assert len(ex.exceptions) == 2 | ||
assert isinstance(ex.exceptions[0], ValueError) | ||
assert isinstance(ex.exceptions[1], TypeError) | ||
|
||
try: | ||
a = Actor.remote() | ||
ray.get(a.f.remote()) | ||
except Exception as ex: | ||
assert isinstance(ex, RayTaskError) | ||
assert isinstance(ex, ExceptionGroup) # noqa: F821 | ||
assert len(ex.exceptions) == 2 | ||
assert isinstance(ex.exceptions[0], ValueError) | ||
assert isinstance(ex.exceptions[1], TypeError) | ||
|
||
|
||
def test_except_star_exception(ray_start_regular): | ||
@ray.remote | ||
def task(): | ||
raise ValueError | ||
|
||
@ray.remote | ||
class Actor: | ||
def f(self): | ||
raise ValueError | ||
|
||
# TODO: Don't use exec() when we only support Python 3.11+ | ||
# Here the exec() is used to avoid SyntaxError for except* for Python < 3.11 | ||
python_code = dedent( | ||
"""\ | ||
try: | ||
ray.get(task.remote()) | ||
except* RayTaskError as ex: | ||
assert isinstance(ex, ExceptionGroup) | ||
assert len(ex.exceptions) == 1 | ||
assert isinstance(ex.exceptions[0], RayTaskError) | ||
assert isinstance(ex.exceptions[0], ValueError) | ||
try: | ||
ray.get(task.remote()) | ||
except* ValueError as ex: | ||
assert isinstance(ex, ExceptionGroup) | ||
assert len(ex.exceptions) == 1 | ||
assert isinstance(ex.exceptions[0], RayTaskError) | ||
assert isinstance(ex.exceptions[0], ValueError) | ||
try: | ||
a = Actor.remote() | ||
ray.get(a.f.remote()) | ||
except* RayTaskError as ex: | ||
assert isinstance(ex, ExceptionGroup) | ||
assert len(ex.exceptions) == 1 | ||
assert isinstance(ex.exceptions[0], RayTaskError) | ||
assert isinstance(ex.exceptions[0], ValueError) | ||
try: | ||
a = Actor.remote() | ||
ray.get(a.f.remote()) | ||
except* ValueError as ex: | ||
assert isinstance(ex, ExceptionGroup) | ||
assert len(ex.exceptions) == 1 | ||
assert isinstance(ex.exceptions[0], RayTaskError) | ||
assert isinstance(ex.exceptions[0], ValueError) | ||
""" | ||
) | ||
exec(python_code) | ||
|
||
|
||
def test_except_star_exceptiongroup(ray_start_regular): | ||
exceptiongroup = ExceptionGroup( # noqa: F821 | ||
"test exceptiongroup", [ValueError(), TypeError()] | ||
) | ||
|
||
@ray.remote | ||
def task(): | ||
raise exceptiongroup | ||
|
||
@ray.remote | ||
class Actor: | ||
def f(self): | ||
raise exceptiongroup | ||
|
||
# TODO: Don't use exec() when we only support Python 3.11+ | ||
# Here the exec() is used to avoid SyntaxError for except* for Python < 3.11 | ||
python_code = dedent( | ||
"""\ | ||
try: | ||
ray.get(task.remote()) | ||
except* RayTaskError as ex: | ||
assert isinstance(ex, ExceptionGroup) | ||
assert len(ex.exceptions) == 2 | ||
assert isinstance(ex.exceptions[0], ValueError) | ||
assert isinstance(ex.exceptions[1], TypeError) | ||
try: | ||
ray.get(task.remote()) | ||
except* ValueError as ex: | ||
assert isinstance(ex, ExceptionGroup) | ||
assert len(ex.exceptions) == 1 | ||
assert isinstance(ex.exceptions[0], ValueError) | ||
except* TypeError as ex: | ||
assert isinstance(ex, ExceptionGroup) | ||
assert len(ex.exceptions) == 1 | ||
assert isinstance(ex.exceptions[0], TypeError) | ||
try: | ||
a = Actor.remote() | ||
ray.get(a.f.remote()) | ||
except* RayTaskError as ex: | ||
assert isinstance(ex, ExceptionGroup) | ||
assert len(ex.exceptions) == 2 | ||
assert isinstance(ex.exceptions[0], ValueError) | ||
assert isinstance(ex.exceptions[1], TypeError) | ||
try: | ||
a = Actor.remote() | ||
ray.get(a.f.remote()) | ||
except* ValueError as ex: | ||
assert isinstance(ex, ExceptionGroup) | ||
assert len(ex.exceptions) == 1 | ||
assert isinstance(ex.exceptions[0], ValueError) | ||
except* TypeError as ex: | ||
assert isinstance(ex, ExceptionGroup) | ||
assert len(ex.exceptions) == 1 | ||
assert isinstance(ex.exceptions[0], TypeError) | ||
""" | ||
) | ||
exec(python_code) | ||
|
||
|
||
if __name__ == "__main__": | ||
if os.environ.get("PARALLEL_CI"): | ||
sys.exit(pytest.main(["-n", "auto", "--boxed", "-vs", __file__])) | ||
else: | ||
sys.exit(pytest.main(["-sv", __file__])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters