Skip to content
This repository was archived by the owner on Nov 2, 2022. It is now read-only.

Commit fc07356

Browse files
authored
Merge pull request #10 from WindSoilder/master
Restrict type of `exc` in split function.
2 parents 6a0a18b + f36f486 commit fc07356

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

exceptiongroup/_tests/test_tools.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
from exceptiongroup import ExceptionGroup, split
23

34

@@ -14,10 +15,9 @@ def raise_error_from_another(out_err, another_err):
1415
raise out_err from e
1516

1617

17-
def test_split_for_none_exception():
18-
matched, unmatched = split(RuntimeError, None)
19-
assert matched is None
20-
assert unmatched is None
18+
def test_split_for_none_exception_should_raise_value_error():
19+
with pytest.raises(TypeError):
20+
matched, unmatched = split(RuntimeError, None)
2121

2222

2323
def test_split_when_all_exception_matched():

exceptiongroup/_tools.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ def split(exc_type, exc, *, match=None):
1717
match (None or func): predicate function to restict the split process,
1818
if the argument is not None, only exceptions with match(exception)
1919
will go into matched part.
20+
21+
Note that if the `exc` is type of ExceptionGroup, then the return
22+
value will be tuple of (ExceptionGroup or None, ExceptionGroup or None)
2023
"""
21-
if exc is None:
22-
return None, None
23-
elif isinstance(exc, ExceptionGroup):
24+
if not isinstance(exc, BaseException):
25+
raise TypeError(
26+
"Argument `exc` should be an instance of BaseException."
27+
)
28+
if isinstance(exc, ExceptionGroup):
2429
matches = []
2530
match_notes = []
2631
rests = []
@@ -38,13 +43,13 @@ def split(exc_type, exc, *, match=None):
3843
elif rests and not matches:
3944
return None, exc
4045
else:
41-
match = copy.copy(exc)
42-
match.exceptions = matches
43-
match.sources = match_notes
44-
rest = copy.copy(exc)
45-
rest.exceptions = rests
46-
rest.sources = rest_notes
47-
return match, rest
46+
matched_group = copy.copy(exc)
47+
matched_group.exceptions = matches
48+
matched_group.sources = match_notes
49+
rest_group = copy.copy(exc)
50+
rest_group.exceptions = rests
51+
rest_group.sources = rest_notes
52+
return matched_group, rest_group
4853
else:
4954
if isinstance(exc, exc_type) and (match is None or match(exc)):
5055
return exc, None

0 commit comments

Comments
 (0)